Spring源码情操陶冶-AnnotationConfigBeanDefinitionParser注解配置解析器

本文承接前文Spring源码情操陶冶-自定义节点的解析,分析spring中的context:annotation-config节点如何被解析

源码概览

BeanDefinitionParser接口的实现方法parse代码如下

@Override
    public BeanDefinition parse(Element element, ParserContext parserContext) {
        Object source = parserContext.extractSource(element);

        // Obtain bean definitions for all relevant BeanPostProcessors.主要的作用就是配置BeanPostProcessors
        Set<BeanDefinitionHolder> processorDefinitions =
                AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), source);

        // Register component for the surrounding <context:annotation-config> element.
        CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
        parserContext.pushContainingComponent(compDefinition);

        // Nest the concrete beans in the surrounding component.
        for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
            parserContext.registerComponent(new BeanComponentDefinition(processorDefinition));
        }

        // Finally register the composite component.
        parserContext.popAndRegisterContainingComponent();

        return null;
    }

由上可述,我们明白了为什么配置了context:component-scan节点等同于不需要配置context:annotation-config节点,此处的代码类同与ComponentScanBeanDefinitionParser#registerComponents方法,所以这里就不赘述了,可见>>>Spring源码情操陶冶-ComponentScanBeanDefinitionParser文件扫描解析器ComponentScanBeanDefinitionParser#registerComponents版块

小结

注册多个BeanPostProcessor接口实现类【供后续spring调用统一接口进行解析,比如>>>Spring源码情操陶冶-AbstractApplicationContext#invokeBeanFactoryPostProcessors可会执行解析@Configuration】具体的有

  • ConfigurationClassPostProcessor解析@Configuration注解类

  • AutowiredAnnotationBeanPostProcessor解析@Autowired/@Value注解

  • RequiredAnnotationBeanPostProcessor解析@Required注解

  • CommonAnnotationBeanPostProcessor解析@Resource注解

  • PersistenceAnnotationBeanPostProcessor解析JPA注解,持久层

转载于:https://www.cnblogs.com/question-sky/p/7020022.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值