spring boot的初始化加载

1、CommandLineRunner
在项目中经常需要进行初始化一些数据(比如缓存等),以便后面调用使用。spring boot可以通过CommandLineRunner接口实现启动加载功能。
@Component
@Order(1) //初始化加载优先级 数字越小优先级越高
public class Init implements CommandLineRunner {

    @Resource
    private IESignInitService eSignInitService;

    @Override
    public void run(String... args) throws Exception {
        eSignInitService.init();
    }
CommandLineRunner 加载会在项目启动完成之后进行加载

2、
@PostConstruct
在类加载的时候,为当前类初始化一些数据,那么可以使用@PostConstruct注解。
@Component
public class StaticConfig {
    @Value("${union.card_ws_url}")
    private String cardWsUrl;

    protected static String CARD_WS_URL;

    @PostConstruct
    public void setValue() {
        CARD_WS_URL = cardWsUrl;
    }
@PostConstruct优先级在@Autowired @Value之后,所以可以获取相关的值



猜你喜欢

转载自www.cnblogs.com/tanyucong/p/11076301.html