Bean的執(zhí)行流程圖
Spring中Bean 的完整生命周期,在學(xué)習(xí)Spring 框架的時候剃斧,理解Spring的Bean的管理的非常重要,下圖是Spring的Bean構(gòu)造的全過程:
Bean 的生命周期
Spring啟動,查找并加載需要被Spring管理的bean燥滑,進行Bean的實例化
Bean實例化后對將Bean屬性注入值
如果Bean實現(xiàn)了BeanNameAware接口的話,Spring將調(diào)用setBeanName()方法香罐,將Bean的id 傳進來
如果Bean實現(xiàn)了BeanFactoryAware接口的話卧波,Spring將調(diào)用setBeanFactory()方法,將BeanFactory容器實例傳進來
如果Bean實現(xiàn)了ApplicationContextAware接口的話庇茫,Spring將調(diào)用Bean的setApplicationContext()方法港粱,將bean所在應(yīng)用上下文引用傳進來。
如果Bean實現(xiàn)了BeanPostProcessor接口旦签,Spring就將調(diào)用他們的BeanPostProcessor.postProcessBeforeInitialization()方法
如果Bean中方法標注注解@PostConstruct查坪,就執(zhí)行該方法
如果Bean 實現(xiàn)了InitializingBean接口,Spring將調(diào)用他們的InitializingBean.afterPropertiesSet()方法宁炫。同時偿曙,如果bean使用init-method聲明了初始化方法,該方法也會被調(diào)用
如果Bean 實現(xiàn)了BeanPostProcessor接口羔巢,Spring就將調(diào)用他們的BeanPostProcessor.postProcessAfterInitialization()()方法望忆。這時,Bean已經(jīng)準備創(chuàng)建完成了竿秆。他們將一直存在應(yīng)用上下文中启摄,直到應(yīng)用上下文被銷毀。
如果Bean中方法標注注解@PreDestroy幽钢,就執(zhí)行該方法
如果bean實現(xiàn)了DisposableBean接口鞋仍,Spring將調(diào)用它的destory()接口方法,同樣搅吁,如果bean使用了destory-method 聲明銷毀方法威创,該方法也會被調(diào)用。
代碼實例驗證Bean 的生命周期
User 類谎懦,實現(xiàn)BeanNameAware肚豺,BeanFactoryAware,ApplicationContextAware界拦,InitializingBean吸申,DisposableBean接口
public class User implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean {
private String userName;
public User() {
System.out.println("User 被實例化!O淼椤=夭辍!");
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
System.out.println("阿福聊聊編程r韧日丹!");
this.userName = userName;
}
public void init() {
System.out.println("執(zhí)行initMethod方法!r窍印哲虾!");
}
@PostConstruct
public void postContruct() {
System.out.println("執(zhí)行@PostConstruct方法");
}
public void destory1() {
System.out.println("執(zhí)行destroyMethod方法1铩!J铡晒旅!");
}
@PreDestroy
public void preDestroy() {
System.out.println("執(zhí)行@PreDestroy 方法!M羲摺7狭怠!");
}
public void afterPropertiesSet() throws Exception {
System.out.println("執(zhí)行InitializingBean.afterPropertiesSet()");
}
public void destroy() throws Exception {
System.out.println("DisposableBean.destroy()");
}
public void setBeanName(String name) {
System.out.println("BeanNameAware.setBeanName()" + "--->BeanName:" + name);
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware.setBeanFactory()");
}
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware.setApplicationContext()" );
}
}
定義后置處理器MyBeanPostProcessor扒寄,實現(xiàn)postProcessBeforeInitialization拴签,postProcessAfterInitialization的方法
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization()--before->"+"初始化beanName:"+beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("MyBeanPostProcessor.postProcessAfterInitialization()--after-->"+"初始化beanName:"+beanName);
return bean;
}
}
定義MyConfigUser,將User ,MyBeanPostProcessor 注冊到Spring容器當(dāng)中旗们,關(guān)于Bean注冊參考SpringIOC匯總
@ComponentScan(basePackages = "zfcoding.beanpostprocessor")
@Configuration
public class MyConfigUser {
@Bean(initMethod = "init",destroyMethod = "destory1")
public User user(){
return new User();
}
}
單元測試
public class BeanProcessTest {
@Test
public void test(){
AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(MyConfigUser.class);
User user = applicationContext.getBean(User.class);
applicationContext.close();
}
}
運行結(jié)果:
User 被實例化r玖ā!I峡省岸梨!
BeanNameAware.setBeanName()--->BeanName:user
BeanFactoryAware.setBeanFactory()
ApplicationContextAware.setApplicationContext()
MyBeanPostProcessor.postProcessBeforeInitialization()--before->初始化beanName:user
執(zhí)行@PostConstruct方法
執(zhí)行InitializingBean.afterPropertiesSet()
執(zhí)行initMethod方法!3淼曹阔!
執(zhí)行@PreDestroy 方法!8襞T叻荨!
DisposableBean.destroy()
執(zhí)行destroyMethod方法I菝住Wズ!鬓长!
文章參考:https://www.cnblogs.com/javazhiyin/p/10905294.html
以上就是Spring Bean 創(chuàng)建的全過程谒拴,完事。
我是阿福涉波,公眾號「阿福聊編程」作者英上,對后端技術(shù)保持學(xué)習(xí)愛好者,我會經(jīng)常更新JAVA技術(shù)文章啤覆,在進階的路上苍日,共勉!
歡迎大家關(guān)注我的公眾號窗声,后臺回復(fù)666相恃,領(lǐng)取Java全套進階實戰(zhàn)課程。