spring源码的xml解析过程-3(spring扩展点)

文章目录


返回到
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

  1. BeanDefinitionRegistryPostProcessor 实现该接口可以完成对beanDifinition的修改
  2. 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 接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值