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初始化源码解析总结
以下是第二篇
5. invokeBeanFactoryPostProcessors(beanFactory);
执行BeanFactoryPostProcessor的方法(在上下文中调用注册为bean的factory processors)
BeanFactoryPostProcessor:(这个接口在之前了解过spring中BeanDefinitionRegistryPostProcessor的作用以及spring中BeanFactoryPostProcessor的作用及原理)BeanFactory的后置处理器。在BeanFactory标准初始化(前面4步)之后执行的;
两个接口:BeanFactoryPostProcessor、BeanDefinitionRegistryPostProcessor
5.1先执行BeanDefinitionRegistryPostProcessor的方法
5.1.1 获取所有的BeanDefinitionRegistryPostProcessor;
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
5.1.2 看先执行实现了PriorityOrdered优先级接口的BeanDefinitionRegistryPostProcessor、
// First, invoke the BeanDefinitionRegistryPostProcessors that implement PriorityOrdered.
5.1.3 再执行实现了Ordered顺序接口的BeanDefinitionRegistryPostProcessor;
// Next, invoke the BeanDefinitionRegistryPostProcessors that implement Ordered.
5.1.4最后执行没有实现任何优先级或者是顺序接口的BeanDefinitionRegistryPostProcessors;
// Finally, invoke all other BeanDefinitionRegistryPostProcessors until no further ones appear.
org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanDefinitionRegistryPostProcessors
com.atguigu.ext.MyBeanDefinitionRegistryPostProcessor
@Override
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
System.out.println("postProcessBeanDefinitionRegistry...bean的数量:"+registry.getBeanDefinitionCount());
// 第一种方式
//RootBeanDefinition beanDefinition = new RootBeanDefinition(Blue.class);
// 第二种方式
AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.rootBeanDefinition(Blue.class).getBeanDefinition();
registry.registerBeanDefinition("hello", beanDefinition);
}
5.2 再执行BeanFactoryPostProcessor的方法
5.2.1 获取所有的BeanFactoryPostProcessor
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
5.2.2先执行实现了PriorityOrdered优先级接口的BeanFactoryPostProcessor
// First, invoke the BeanFactoryPostProcessors that implement PriorityOrdered.
5.2.3 再执行实现了Ordered顺序接口的BeanFactoryPostProcessor;
// Next, invoke the BeanFactoryPostProcessors that implement Ordered.
5.2.4 最后执行没有实现任何优先级或者是顺序接口的BeanFactoryPostProcessor;
// Finally, invoke all other BeanFactoryPostProcessors.
org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(java.util.Collection<? extends org.springframework.beans.factory.config.BeanFactoryPostProcessor>, org.springframework.beans.factory.config.ConfigurableListableBeanFactory)
com.atguigu.ext.MyBeanDefinitionRegistryPostProcessor
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanDefinitionRegistryPostProcessor...bean的数量:"+beanFactory.getBeanDefinitionCount());
}
com.atguigu.ext.MyBeanFactoryPostProcessor
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
System.out.println("MyBeanFactoryPostProcessor...postProcessBeanFactory...");
int count = beanFactory.getBeanDefinitionCount();
String[] names = beanFactory.getBeanDefinitionNames();
System.out.println("当前BeanFactory中有"+count+" 个Bean");
System.out.println(Arrays.asList(names));
}
可以看到我们自定义的两个Bean
MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor
MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor
都在这个过程中执行了