先看一张图: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
运行流程: