詳細(xì)SpringBoot生命周期接口的使用
一 背景
最近在做一個(gè)項(xiàng)目啟動(dòng)時(shí)加載配置到SpringBoot容器中的功能,看到了Spring中有很多在容器初始化時(shí)的接口,這些接口或注解包括InitializingBean、@PostConstruct、SmartInitializingSingleton、BeanPostProcess
等等,這么多都可以在初始化時(shí)使用,但是他們有什么區(qū)別呢,下面就來說說他們之間的區(qū)別
二 SpringBoot 生命周期接口
@PostConstruct
這個(gè)注解在實(shí)際的開發(fā)中有較多的用到
@Component public class TestP { @PostConstruct public void test() { System.out.println("@PostConstruct"); }
這樣在容器啟動(dòng)過程中就回執(zhí)行打印,看起來他像是對(duì)象的構(gòu)造方法,其實(shí)他的作用是,當(dāng)一個(gè)對(duì)象A中存在@Autowire修飾的依賴B時(shí),正常來說,對(duì)象會(huì)先執(zhí)行自己的構(gòu)造方法,然后再去注入依賴,但是我們現(xiàn)在有一種情況,在對(duì)象實(shí)例化時(shí),要執(zhí)行構(gòu)造方法,但是構(gòu)造方法中用到依賴B,這個(gè)時(shí)候用 @PostConstruct就能解決這個(gè)問題。
public class TestP implements InitializingBean, SmartInitializingSingleton { @Autowired Context context; ``` public TestP () { System.out.println(context); } ``` @PostConstruct public void test() { System.out.println(context); System.out.println("@PostConstruct"); }
輸出
null
com.example.demo1.bean.Context@4f96a58
@PostConstruct
InitializingBean
他提供bean初始化的回調(diào)處理功能,看下這個(gè)接口的源碼
protected void invokeInitMethods(String beanName, final Object bean, RootBeanDefinition mbd) throws Throwable { boolean isInitializingBean = (bean instanceof InitializingBean); //判斷該bean是否實(shí)現(xiàn)了實(shí)現(xiàn)了InitializingBean接口,如果實(shí)現(xiàn)了InitializingBean接口,則只掉調(diào)用bean的afterPropertiesSet方法 if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) { if (logger.isDebugEnabled()) { logger.debug("Invoking afterPropertiesSet() on bean with name '" + beanName + "'"); } if (System.getSecurityManager() != null) { try { AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() { public Object run() throws Exception { //直接調(diào)用afterPropertiesSet ((InitializingBean) bean).afterPropertiesSet(); return null; } },getAccessControlContext()); } catch (PrivilegedActionException pae) { throw pae.getException(); } } else { //直接調(diào)用afterPropertiesSet ((InitializingBean) bean).afterPropertiesSet(); } } if (mbd != null) { String initMethodName = mbd.getInitMethodName(); //判斷是否指定了init-method方法,如果指定了init-method方法,則再調(diào)用制定的init-method if (initMethodName != null && !(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) && !mbd.isExternallyManagedInitMethod(initMethodName)) { //進(jìn)一步查看該方法的源碼,可以發(fā)現(xiàn)init-method方法中指定的方法是通過反射實(shí)現(xiàn) invokeCustomInitMethod(beanName, bean, mbd); } }
可以看到,只要實(shí)現(xiàn)了這個(gè)接口的bean都會(huì)執(zhí)行里面的
afterPropertiesSet
方法,那他和@PostConstruct
有什么區(qū)別呢,區(qū)別是他們的生命周期排序不同,@PostConstruct
是對(duì)單個(gè)Bean實(shí)例化時(shí)使用,而InitializingBean
是在所有spring bean實(shí)例化后對(duì)bean進(jìn)行處理,?致的步驟是這樣的實(shí)例化bean,這?會(huì)調(diào)?構(gòu)造?法
填充屬性,就是依賴注?
初始化bean,
- 調(diào)?后置處理器,其中會(huì)執(zhí)?@PostConstruct注解?法
- 執(zhí)?bean的?命周期中的初始化回調(diào)?法,也就是InitializingBean接?的afterPropertiesSet()?法
BeanPostProcess
這個(gè)接口主要是對(duì)注冊(cè)的bean中的屬性進(jìn)行初始化時(shí)的修改
@Component public class BeanPostTest implements BeanPostProcessor { @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("執(zhí)行1--------------- - " + bean.getClass().getName() + " - " + beanName); return null; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("執(zhí)行2--------------- - " + bean.getClass().getName() + " - " + beanName); return null; } }
他和
InitializingBean
區(qū)別一個(gè)是執(zhí)行順序不同
他有兩個(gè)方法分別在在InitializingBean
執(zhí)行前執(zhí)行后執(zhí)行,第二個(gè)區(qū)別就是InitializingBean
是一個(gè)可以進(jìn)一步調(diào)整bean的實(shí)例的接口,不過并不是每個(gè)類都會(huì)來執(zhí)行這個(gè)接口方法,這個(gè)接口只針對(duì)當(dāng)前實(shí)現(xiàn)類,而BeanPostProcess是針對(duì)所有bean的,每一個(gè)bean被注冊(cè),都會(huì)被執(zhí)行一次這兩個(gè)方法
SmartInitializingSingleton
這個(gè)是在spring 4.1版本才推出的接口,他的執(zhí)行時(shí)時(shí)機(jī)是在單例預(yù)實(shí)例化階段結(jié)束時(shí)調(diào)用,并保證已經(jīng)創(chuàng)建了所有常規(guī)單例bean,所以他的執(zhí)行順序是比較靠后的,考慮到一些bean的注冊(cè)及修改使用
SmartInitializingSingleton
是比較穩(wěn)妥的一種方式``` @Component public class GetuiAccountConfigInit implements SmartInitializingSingleton { @Override public void afterSingletonsInstantiated() { } } ```
Commandlinerunner 這個(gè)接口是springBoot的接口,他是在所有bean都加載后才會(huì)執(zhí)行的,如果實(shí)現(xiàn)這個(gè)接口,可以很好的在啟動(dòng)時(shí)初始化資源,因?yàn)樗械腷ean都可以使用了
@Component public class Runner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("執(zhí)行初始化"); } }
如果我們要執(zhí)行的程序有順序要求,還可以使用@Order注解
@Component @Order(1) public class OrderRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The OrderRunner1 start to initialize ..."); } } @Component @Order(2) public class OrderRunner1 implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The OrderRunner2 start to initialize ..."); } }
三 后記
Spring由于設(shè)計(jì)上比較靈活所以留了很多接口,讓開發(fā)人員進(jìn)行拓展,這本身是一個(gè)很好的學(xué)習(xí)借鑒的經(jīng)驗(yàn),現(xiàn)在大部分的開發(fā)使用的都是spring的框架,這就要求我們做一些設(shè)計(jì)時(shí)要了解框架的特性,才能進(jìn)行更好的設(shè)計(jì),上面的幾個(gè)接口是相對(duì)來說比較常用的接口,里面的技術(shù)細(xì)節(jié)也值得推敲,希望對(duì)大家有所幫助。
到此這篇關(guān)于詳細(xì)SpringBoot生命周期接口的使用的文章就介紹到這了,更多相關(guān)SpringBoot生命周期接口內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效
這篇文章主要介紹了解決IDEA maven 項(xiàng)目修改代碼不生效,mvn clean、install后才生效的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09使用mybatisPlus生成oracle自增序列遇到的坑及解決
這篇文章主要介紹了使用mybatisPlus生成oracle自增序列遇到的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java使用IntelliJ IDEA連接MySQL的詳細(xì)教程
這篇文章主要給大家介紹了關(guān)于Java使用IntelliJ IDEA連接MySQL的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04關(guān)于任務(wù)調(diào)度框架quartz使用(異常處理,解決恢復(fù)后多次調(diào)度處理)
這篇文章主要介紹了關(guān)于任務(wù)調(diào)度框架quartz使用(異常處理,解決恢復(fù)后多次調(diào)度處理),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Mybatis在注解上如何實(shí)現(xiàn)動(dòng)態(tài)SQL
這篇文章主要介紹了Mybatis在注解上如何實(shí)現(xiàn)動(dòng)態(tài)SQL,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Java的long和bigint長(zhǎng)度對(duì)比詳解
在本文中小編給大家分享了關(guān)于Java的long和bigint長(zhǎng)度比較的知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們學(xué)習(xí)參考下。2019-07-07