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

posted @ 2017-06-15 20:59  南柯问天  阅读(1334)  评论(0编辑  收藏  举报