Bean生命周期
Steps:
- Spring进行Bean的实例化;
- 将Bean的引入和值注入到Bean的属性中;
- 如果Bean实现了BeanNameAware接口的话,Spring将Bean的Id传递给setBeanName()方法;
- 如果Bean实现了BeanFactoryAware接口的话,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入
- 如果Bean实现了ApplicationContextAware接口的话,Spring将调用Bean的setApplicationContext()方法,将bean所在应用上下文引用传入进来。
- 如果Bean实现了BeanPostProcessor接口,Spring就将调用他们的postProcessBeforeInitialization()方法。
- 如果Bean实现了InitializingBean接口,Spring将调用他们的afterPropertiesSet()方法。类似的,如果bean使用init-method声明了初始化方法,该方法也会被调用
- 如果Bean 实现了BeanPostProcessor接口,Spring就将调用他们的postProcessAfterInitialization()方法。
- 在容器中可以使用
- 如果bean实现了DisposableBean接口,Spring将调用它的destory()接口方法,同样,如果bean使用了destory-method 声明销毁方法,该方法也会被调用。

顺序:
- BeanNameAware
- BeanFactoryAware
- ApplicationContextAware
- BeanPostProcessor钩子:postProcessBeforeInitialization
- InitializingBean:afterPropertiesSet;
- @PostConstruct
- BeanPostProcessor钩子:postProcessAfterInitialization.