码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot的初始化加载

时间:2019-06-24 12:16:41      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:exception   static   类加载   spring   string   command   数字   cte   source   

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之后,所以可以获取相关的值



spring boot的初始化加载

标签:exception   static   类加载   spring   string   command   数字   cte   source   

原文地址:https://www.cnblogs.com/tanyucong/p/11076301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!