1. beanFactory容器的创建及初始化
参考:spring ioc容器创建与bean初始化源码解析1:beanFactory容器的创建及初始化
2. 在beanFactory容器初始化之后定制和修改BeanFactory内容
参考:spring ioc容器创建与bean初始化源码解析2:在beanFactory容器初始化之后定制和修改BeanFactory内容
3. 注册BeanPostProcessor(作用在bean创建的过程中进行拦截,从而执行指定逻辑)
参考:spring ioc容器创建与bean初始化源码解析3:注册BeanPostProcessor(作用在bean创建的过程中进行拦截,从而执行指定逻辑)
4. 初始化MessageSource ApplicationEventMulticaster ApplicationListener
参考:spring ioc容器创建与bean初始化源码解析4:初始化MessageSource ApplicationEventMulticaster ApplicationListener
5. 初始化所有剩余未初始化的单实例bean及最终完成容器创建
参考:spring ioc容器创建与bean初始化源码解析5:初始化所有剩余未初始化的单实例bean及最终完成容器创建
6. spring ioc容器创建与bean初始化源码解析总结
参考:spring ioc容器创建与bean初始化源码解析总结
以下是第三篇
6. registerBeanPostProcessors(beanFactory);
注册拦截bean创建的 bean processors
注册BeanPostProcessor(Bean的后置处理器)来intercept bean creation
BeanPostProcessor类图
DestructionAwareBeanPostProcessor
InstantiationAwareBeanPostProcessor
SmartInstantiationAwareBeanPostProcessor
MergedBeanDefinitionPostProcessor[internalPostProcessors]
不同接口类型的BeanPostProcessor;在Bean创建前后的执行时机是不一样的
6.1 获取所有的 BeanPostProcessor;后置处理器都默认可以通过PriorityOrdered、Ordered接口来执行优先级
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
6.2 先注册PriorityOrdered优先级接口的BeanPostProcessor;
把每一个BeanPostProcessor;添加到BeanFactory中
beanFactory.addBeanPostProcessor(postProcessor);
// First, register the BeanPostProcessors that implement PriorityOrdered.
6.3 再注册Ordered接口的
// Next, register the BeanPostProcessors that implement Ordered.
6.4 最后注册没有实现任何优先级接口的
// Now, register all regular BeanPostProcessors.
6.5 最终注册MergedBeanDefinitionPostProcessor;
if (pp instanceof MergedBeanDefinitionPostProcessor) {
internalPostProcessors.add(pp);
}
// Finally, re-register all internal BeanPostProcessors.
sortPostProcessors(internalPostProcessors, beanFactory);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
6.6 注册一个ApplicationListenerDetector;来在Bean创建完成后检查是否是ApplicationListener,如果是 applicationContext.addApplicationListener((ApplicationListener<?>) bean);
// Re-register post-processor for detecting inner beans as ApplicationListeners,
// moving it to the end of the processor chain (for picking up proxies etc).
beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(applicationContext));