返回到
org.springframework.context.support.AbstractApplicationContext#refresh
// xml标签解析、class类上的注解解析、对相关注解支持的处理器注册 如 @Autowired、@Controller
// 将xml标签属性、注解属性 都封装成 beanDefinition,并对 beanDefinition 进行注册
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
invokeBeanFactoryPostProcessors(beanFactory);
org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors
PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory,
getBeanFactoryPostProcessors());
org.springframework.context.support.PostProcessorRegistrationDelegate#invokeBeanFactoryPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanFactoryPostProcessor>)
// 循环所有 BeanFactoryPostProcessor 的 实现类
for (BeanFactoryPostProcessor postProcessor : beanFactoryPostProcessors) {
if (postProcessor instanceof BeanDefinitionRegistryPostProcessor) {
BeanDefinitionRegistryPostProcessor registryProcessor =
(BeanDefinitionRegistryPostProcessor) postProcessor;
registryProcessor.postProcessBeanDefinitionRegistry(registry);
registryProcessors.add(registryProcessor);
}
else {
regularPostProcessors.add(postProcessor);
}
}
// 获取所有 BeanDefinitionRegistryPostProcessor 类的子类的bean名字
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanDefinitionRegistryPostProcessor.class, true, false);
// 循环调用 BeanDefinitionRegistryPostProcessor 类的实现类的方法 postProcessBeanDefinitionRegistry 方法
invokeBeanDefinitionRegistryPostProcessors(currentRegistryProcessors, registry);
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanFactoryPostProcessor.class, true, false);
// 循环调用 BeanFactoryPostProcessor 类的实现类的方法 postProcessBeanFactory 方法
invokeBeanFactoryPostProcessors(priorityOrderedPostProcessors, beanFactory);
BeanDefinitionRegistryPostProcessor
类是继承BeanFactoryPostProcessor
类的,注意:其中ConfigurationClassPostProcessor
类,就是BeanDefinitionRegistryPostProcessor
的子类,而这个类是 @Configuration 注解的处理类,这个类会在扫描包处理时,进行注册的
重点类-1
BeanDefinitionRegistryPostProcessor 实现该接口可以完成对beanDifinition的修改
BeanFactoryPostProcessor 实现该接口可以完成对 beanDifinition 的修改
优先级问题:
spring 内部先处理 PriorityOrdered 实现类 再处理 Ordered 实现类
PriorityOrdered 类为空接口,这样做的目的是在需要地方,可能根据接口的类型,获取接口的所有实现类
测试
@Component
public class My1BeanFactoryPostProcessor implements BeanFactoryPostProcessor {
private Logger logger = LoggerFactory.getLogger(My1BeanFactoryPostProcessor.class);
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
logger.info("BeanFactoryPostProcessor postProcessBeanFactory My1BeanFactoryPostProcessor");
// 根据bean id 获取 bean 的 BeanDefinition
BeanDefinition bd = arg0.getBeanDefinition("jackstudent");
// 获取bean 对象中的所有属性
MutablePropertyValues mpv = bd.getPropertyValues();
// 修改 bean 属性值
mpv.addPropertyValue("password", "fhdksafhldsafjl-vip");
}
}
MutablePropertyValues 类中的
List<PropertyValue> propertyValueList
属性,存放的就是 bean 类中的属性字段信息,该类 MutablePropertyValues 对象存放于 BeanDefinition 中
返回到
org.springframework.context.support.AbstractApplicationContext#refresh
registerBeanPostProcessors(beanFactory);
org.springframework.context.support.AbstractApplicationContext#registerBeanPostProcessors
PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, org.springframework.context.support.AbstractApplicationContext)
String[] postProcessorNames =
beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
....
// 将 BeanPostProcessor 接口的所有实现类都添加到 ConfigurableListableBeanFactory 对象中
registerBeanPostProcessors(beanFactory, nonOrderedPostProcessors);
registerBeanPostProcessors(beanFactory, internalPostProcessors);
MergedBeanDefinitionPostProcessor 是 BeanPostProcessor 类的子类
org.springframework.context.support.PostProcessorRegistrationDelegate#registerBeanPostProcessors(org.springframework.beans.factory.config.ConfigurableListableBeanFactory, java.util.List<org.springframework.beans.factory.config.BeanPostProcessor>)
private static void registerBeanPostProcessors(
ConfigurableListableBeanFactory beanFactory,
List<BeanPostProcessor> postProcessors) {
for (BeanPostProcessor postProcessor : postProcessors) {
beanFactory.addBeanPostProcessor(postProcessor);
}
}
重点类-2
MergedBeanDefinitionPostProcessor 和 BeanPostProcessor 接口