前言

在没有注释的情况下的流程节点都是针对单个 Bean 而言的, 但是 BeanPostProcessor 是针对所有 Bean 而言的.
即使你定义了 ApplicationContextAware 接口, 但是有时候并不会调用, 这要根据你的 IoC 容器来决定.
我们知道, Spring IoC 容器最低要求是实现 BeanFactory 接口, 而不是实现ApplicationContext 接口.
对于那些没有实现 ApplicationContext 接口的容器, 在生命周期对应的 ApplicationContextAware 定义的方法不会被调用.
ApplicationContextAware 功能由 ApplicationContextAwareProcessor 实现.
BeanNameAware, BeanFactoryAware 在开发中基本不会用到.
后置处理器
BeanPostProcessor
BeanFactory 在初始化每个 Bean 之后, 会调用所有注册的 BeanPostProcessor, BeanPostProcessor 可以对原始实例进行包装代理等处理, 并返回处理后的新实例. 实际放入 BeanFactory 容器的是处理后的实例.
作用是对 Bean 进行包装
public interface BeanPostProcessor {default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {return bean;}default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {return bean;}}
postProcessBeforeInitialization: Apply this BeanPostProcessor to the given new bean instance before any bean initialization callbacks (like InitializingBean’s afterPropertiesSet or a custom init-method). The bean will already be populated with property values.
The returned bean instance may be a wrapper around the original.postProcessAfterInitialization: Apply this BeanPostProcessor to the given new bean instance after any bean initialization callbacks (like InitializingBean’s_ _afterPropertiesSet or a custom init-method). The bean will already be populated with property values.
The returned bean instance may be a wrapper around the original.


BeanFactoryPostProcessor
在 BeanFactory 标准化后执行 (所有的 bean 定义已经保存加载到 BeanFactory 但并未创建任何 bean 对象, 获取不到实例, 但是可以知道 bean 中定义的数量以及每个 bean 定义的名字)
这个后置处理器的显著的作用就是可以动态定制容器中符合自己要求的 bean, 或者加入自己的配置替代原先的配置.
可以配置多个BeanFactoryPostProcessor, 并通过实现Ordered接口控制顺序.
BeanFactoryPostProcessor 的典型应用: PropertyPlaceholderConfigurer.
BeanDefinitionRegistryPostProcessor
在 BeanFactoryPostProcessor 执行之前执行 (在所有符合规则的 bean 的定义信息将要加载, 但未创建 bean 实例, 可以额外的给容器中添加一个 bean)
作用是可以额外的添加组件
参考文档
Bean 生命周期
Bean 生命周期
Spring Aware 到底是个啥?
