先看一張圖:spring4.x 企業實戰spring
spring版本:4.3.17
一、bean自身的生命週期接口ide
1.一、實現 InitializingBean、DisposableBean 接口
這2個接口,會要求你實現2個方法post
@Component public class BeanSelf implements InitializingBean, DisposableBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("afterPropertiesSet"); } @Override public void destroy() throws Exception { System.out.println("destroy"); } }
afterPropertieseSet 會在屬性設置完畢,調用,這裏的屬性設置,指的是Bean的依賴注入。好比。在 BeanSelf中注入 BeanSelf2this
private BeanSelf2 beanSelf2 @Autowired public void setBeanSelf2(BeanSelf2 beanSelf2) { System.out.println("注入BeanSelf2"); this.beanSelf2 = beanSelf2 }
運行結果spa
注入 beanSelf2 afterPropertiesSet destroy
1.二、使用 @PostConstruct、 @PreDestroy 註解
正如其名:在構造器以後, 即在銷燬以前。code
public class BeanSelf implements InitializingBean, DisposableBean{ private BeanSelf2 beanSelf2; private BeanSelf3 beanSelf3; public BeanSelf(BeanSelf2 beanSelf2) { System.out.println("構造器注入 beanSelf2"); this.beanSelf2 = beanSelf2; } @Autowired public void setBeanSelf3(BeanSelf3 beanSelf3) { System.out.println("屬性注入 beanSelf3"); this.beanSelf3 = beanSelf3; } @PostConstruct public void init() { System.out.println("PostConstruct"); } @PreDestroy public void initDestroy2() { System.out.println("PreDestroy"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("afterPropertiesSet"); } @Override public void destroy() throws Exception { System.out.println("destroy"); } }
運行效果blog
構造器注入 beanSelf2 屬性注入 beanSelf3 PostConstruct --- 很明顯 @PostConstruct 是在構造器以後注入 beanSelf2 afterPropertiesSet --- 在 PostConstruct 以後 PreDestroy --- 很明顯,是在銷燬以前調用的 destroy
小總結:無論是@PostConstruct 或者 實現InitializingBean接口。 都是在Bean實例化完成以後才調用的。接口
二、beanFactory工廠接口,只調用一次生命週期
@Component public class MyBeanFactory implements BeanFactoryPostProcessor { @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { Iterator<String> iterator = configurableListableBeanFactory.getBeanNamesIterator(); BeanSelf2 beanSelf = (BeanSelf2) configurableListableBeanFactory.getBean("beanSelf2"); beanSelf.add(); System.out.println("beanFactoryPostProcessor"); } } @Component public class BeanSelf2 implements InitializingBean{ public void add() { System.out.println("add"); } @Override public void afterPropertiesSet() throws Exception { System.out.println("beanSelf2 afterPropertiesSet"); } }
這個接口的方法會在實例化以前調用。 在postProcessBeanFactory 中,對BeanSelf2提早進行初始化,並調用add方法。ip
beanSelf2 afterPropertiesSet -- 調用beanSelf2的afterPropertiesSet方法 add beanFactoryPostProcessor 構造器注入 beanSelf2 屬性注入 beanSelf3 PostConstruct afterPropertiesSet PreDestroy
BeanFactoryPostProcessor 顧名思義,在這個方法裏面能夠拿到全部裝載的Bean,並在初始化以前對某些Bean進行修改。(此時Bean還未初始化)
三、BeanPostProcessor接口在每一個Bean實例以前,都會調用。若是Bean已實例化則不會diaoy
@Component public class MyBeanPostProcessor implements BeanPostProcessor{ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName +" = postProcessBeforeInitialization" ); if("beanSelf2".equals(beanName)) { return null; } return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println(beanName +" = postProcessAfterInitialization"); return bean; } }
上面這段代碼的意思是,在初始化以前,將BeanSelf2 和 BeanSel3 置爲null。可是,BeanSelf2不會走到這段代碼內,由於在接口BeanFactoryPostProcessor 中,將BeanSelf2提早初始化了。因此輸出結果以下
beanSelf2 afterPropertiesSet --- BeanFactoryPostProcessor中提早初始化 add --- 調用BeanSelf2的add方法 beanFactoryPostProcessor --- 在postProcessBeanFactory 中打印 beanConfig = postProcessBeforeInitialization --調用 BeanPostProcessor beanConfig = postProcessAfterInitialization --調用 BeanPostProcessor BeanSelf 構造器注入 beanSelf2 --BeanSelf 構造器注入屬性 beanSelf3 = postProcessBeforeInitialization -- 在實例化以前調用 BeanSelf 屬性注入 beanSelf3 -- 注入以前,BeanSelf3還沒實例化,因此在以前調用 BeanPostProcessor beanSelf = postProcessBeforeInitialization --- beanSelf 在屬性設置完畢後,即初始化完畢 調用 BeanPostProcessor#postProcessBeforeInitialization() BeanSelf PostConstruct -- 調用被 @PostConstruct 註解聲明的方法 afterPropertiesSet -- 調用 InitializingBean 接口實現的方法 beanSelf = postProcessAfterInitialization beanSelf2 com.xhn3.BeanSelf2@33cb5951 beanSelf3 null -- 在BeanPostProcessor中返回null。因此這邊是null BeanSelf PreDestroy destroy
小總結:BeanPostProcessor#postProcessBeforeInitialization在init-method以前調用,在屬性設置完畢後調用。BeanPostProcessor#postProcessAfterInitialization 在InitializingBean#afterPropertiesSet後調用。
四、BeanDefinitionRegistryPostProcessor 註冊Bean的接口,在BeanFactoryPostProcesso前調用
@Component public class MyBeanRegister implements BeanDefinitionRegistryPostProcessor { @Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { } }
在該方法裏面能夠直接註冊bean。能夠提早實例化Bean
運行流程: