Spring啟動(dòng)時(shí)實(shí)現(xiàn)初始化的幾種方案
準(zhǔn)確的說是spring容器實(shí)例化完成后,幾種初始化的方式。為什么這么說呢?
下看面示例:
@Slf4j @Component public class InitBeanDemo { @Autowired private Environment env; public InitBeanDemo() { log.info("DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); }
示例是在bean的構(gòu)造方法里做一些初始化的工作,示例比較簡單只做了日志打印。是理想很豐滿,現(xiàn)實(shí)很骨感,報(bào)錯(cuò)了:
Constructor threw exception; nested exception is java.lang.NullPointerException
原因是,Environment尚未初始化完成。
接下來我們來探索一下 有哪些初始化方式能滿足上面示例的需求。
構(gòu)造方法里初始化
可以正常運(yùn)行,在所有初始化方式里執(zhí)行時(shí)機(jī)最早。
原理是在InitBeanDemo實(shí)例化前就實(shí)例化了Environment。
@Component public class InitBeanDemo { private final Environment env; @Autowired public InitBeanDemo (Environment environment) { this.env = environment; log.info("Constructor DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("Constructor ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); } }
常規(guī)三件套
常規(guī)三件套:@PostConstruct、InitializingBean、initMethod。
如果你愿意的話,三種方式可以在同一個(gè)Bean下同時(shí)使用,執(zhí)行的優(yōu)先級(jí)@PostConstruct > InitializingBean > initMethod。
@PostConstruct注解
在一個(gè)可以被掃描到Bean里,添加一個(gè)public void xxx()方法并加上@PostConstruct注解,方法里編寫需要初始化的邏輯。
同一個(gè)應(yīng)用程序里可以有多個(gè)@PostConstruct注解,同一個(gè)Bean里也可以有多個(gè)@PostConstruct注解。
@Slf4j @Component public class InitBeanDemo { @Autowired private Environment env; @PostConstruct public void init() { log.info("@PostConstruct DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("@PostConstruct ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); }
實(shí)現(xiàn)InitializingBean接口
實(shí)現(xiàn)InitializingBean接口,在afterPropertiesSet() 方法里編寫需要初始化的邏輯。
同一個(gè)應(yīng)用程序里可以有多個(gè)實(shí)現(xiàn)InitializingBean接口的類,執(zhí)行時(shí)機(jī)會(huì)按類名的自然順序排序。
@Slf4j @Component public class InitBeanDemo implements InitializingBean { @Autowired private Environment env; @Override public void afterPropertiesSet() throws Exception { log.info("InitializingBean DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("InitializingBean ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); } }
指定Bean的initMethod方法
使用@Bean注解的initMethod屬性可用于Bean的初始化后執(zhí)行的方法。
initMethod必須是public void 的無參構(gòu)造方法。
@Slf4j public class InitBeanDemo implements InitializingBean { @Autowired private Environment env; public void initMethod() { log.info("initMethod DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("initMethod ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); }
@Configuration public class InitBeanConfig { @Bean(initMethod="initMethod") public InitBeanDemo initBeanDemo () { return new InitBeanDemo(); } }
等同于 在XML 配置中的init-method屬性:
<bean id="initBeanDemo" class="com.xxx.InitBeanDemo" init-method="initMethod"></bean>
自定義ApplicationListener監(jiān)聽
兩種方式,一種實(shí)現(xiàn)接口,另一種使用注解。
實(shí)現(xiàn)ApplicationListener接口
監(jiān)聽ContextRefreshedEvent事件。
@Slf4j @Component public class InitBeanDemo implements ApplicationListener<ContextRefreshedEvent>{ @Autowired private Environment env; @Override public void onApplicationEvent(ContextRefreshedEvent event) { log.info("ApplicationListener DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("ApplicationListener ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); } }
@EventListener注釋
方法參數(shù)里指定ContextRefreshedEvent事件。
@Slf4j @Component public class InitBeanDemo { @Autowired private Environment env; @EventListener public void onApplicationEvent2(ContextRefreshedEvent event) { log.info("@EventListener DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("@EventListener ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); } }
Spring Boot提供的初始化接口
ApplicationRunner接口
@Slf4j @Component public class InitBeanDemo implements ApplicationRunner { @Autowired private Environment env; @Override public void run(ApplicationArguments args) throws Exception { log.info("ApplicationRunner: {}", args); log.info("ApplicationRunner: {}", args.getOptionNames()); log.info("ApplicationRunner DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("ApplicationRunner ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); } }
CommandLineRunner接口
可以在同一個(gè)應(yīng)用程序上下文中定義多個(gè)CommandLineRunner bean,并且可以使用@Ordered接口或@Order注釋進(jìn)行排序。
@Slf4j @Component public class InitBeanDemo implements CommandLineRunner { @Autowired private Environment env; @Override public void run(String... args) throws Exception { log.info("CommandLineRunner: {}", args); log.info("CommandLineRunner DefaultProfiles: {}", Arrays.asList(env.getDefaultProfiles())); log.info("CommandLineRunner ActiveProfiles: {}", Arrays.asList(env.getActiveProfiles())); } }
在同一個(gè)Bean里使用以上初始化方式的執(zhí)行先后順序
在同一個(gè)Bean里使用以上初始化方式,運(yùn)行的日志片段:
2021-06-07 11:24:41|INFO |main|c.c.s.s.t.ConstructorInitDemo|Constructor DefaultProfiles: [default]
2021-06-07 11:24:41|INFO |main|c.c.s.s.t.ConstructorInitDemo|Constructor ActiveProfiles: [sit]2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|@PostConstruct DefaultProfiles: [default]
2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|@PostConstruct ActiveProfiles: [sit]
2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|InitializingBean DefaultProfiles: [default]
2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|InitializingBean ActiveProfiles: [sit]
2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|initMethod DefaultProfiles: [default]
2021-06-07 11:24:42|INFO |main|c.c.s.s.test.InitBeanDemo|initMethod ActiveProfiles: [sit]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|@EventListener DefaultProfiles: [default]
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|@EventListener ActiveProfiles: [sit]
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationListener DefaultProfiles: [default]
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationListener ActiveProfiles: [sit]
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationRunner: org.springframework.boot.DefaultApplicationArguments@68bef3df
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationRunner: []
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationRunner DefaultProfiles: [default]
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|ApplicationRunner ActiveProfiles: [sit]2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|CommandLineRunner: {}
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|CommandLineRunner DefaultProfiles: [default]
2021-06-07 11:24:44|INFO |main|c.c.s.s.test.InitBeanDemo|CommandLineRunner ActiveProfiles: [sit]
也即 整篇文章整理的先后順序。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
ssh框架實(shí)現(xiàn)文件上傳下載實(shí)例代碼
本篇文章主要介紹了ssh框架文件上傳下載實(shí)例代碼,實(shí)例分析了Spring+struts+Hibernate的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2017-03-03基于Java文件輸入輸出流實(shí)現(xiàn)文件上傳下載功能
這篇文章主要為大家詳細(xì)介紹了基于Java文件輸入輸出流實(shí)現(xiàn)文件上傳下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04SpringBoot 指標(biāo)監(jiān)控actuator的專題
未來每一個(gè)微服務(wù)在云上部署以后,我們都需要對(duì)其進(jìn)行監(jiān)控、追蹤、審計(jì)、控制等。SpringBoot就抽取了Actuator場景,使得我們每個(gè)微服務(wù)快速引用即可獲得生產(chǎn)級(jí)別的應(yīng)用監(jiān)控、審計(jì)等功能,通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下2021-11-11Java網(wǎng)絡(luò)通信中ServerSocket的設(shè)計(jì)優(yōu)化方案
今天小編就為大家分享一篇關(guān)于Java網(wǎng)絡(luò)通信中ServerSocket的設(shè)計(jì)優(yōu)化方案,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04微信小程序+后端(java)實(shí)現(xiàn)開發(fā)
這篇文章主要介紹了微信小程序+后端(java)實(shí)現(xiàn)開發(fā),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04Spring?@Cacheable注解類內(nèi)部調(diào)用失效的解決方案
這篇文章主要介紹了Spring?@Cacheable注解類內(nèi)部調(diào)用失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01Java中的ScheduledThreadPoolExecutor定時(shí)任務(wù)詳解
這篇文章主要介紹了Java中的ScheduledThreadPoolExecutor詳解,??ScheduledThreadPoolExecutor?繼承自?ThreadPoolExecutor,它主要用來在給定的延遲之后運(yùn)行任務(wù),或者定期執(zhí)行任務(wù),ScheduledThreadPoolExecutor?的功能與?Timer?類似<BR>,需要的朋友可以參考下2023-12-12關(guān)于報(bào)錯(cuò)IDEA Terminated with exit code
如果在IDEA構(gòu)建項(xiàng)目時(shí)遇到下面這樣的報(bào)錯(cuò)IDEA Terminated with exit code 1,那必然是Maven的設(shè)置參數(shù)重置了,導(dǎo)致下載錯(cuò)誤引起的,本文給大家分享兩種解決方法,需要的朋友可以參考下2022-08-08