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注解,持久层