springMVC的配置文件中经常见到<context:annotation-config/>,那么这句话的作用到底是什么呢?

现在的注解非常方便,但是系统如何才能识别注解呢,这就需要相应的处理程序了,springMVC使用AnnotationBeanPostProcessor让系统能够识别相应的注解

例如:如果你想使用Autowired注解,那么必须先在spring容器中声明AutoWiredAnnotationBeanPostProccessor的实例,如要使用@Required注解就必须先声明RequiredAnnotationBeanPostProccessor,一般的方法是在配置文件挨个定义:

//@AutoWired注解处理器
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
//@Required注解处理器
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

然而对于这些常见的注解,每次显示定义很麻烦,所以springMVC给我们提供了隐式定义的方法,即<context:annotation-config/>标签。

下面看看<context:annotation-config/>标签注册过程及注册了哪些实例到容器。

首先找到实例定义接口BeanDefinitionParser

public class AnnotationConfigBeanDefinitionParser implements BeanDefinitionParser {
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
     //得到一个set集合,此集合中就是各个注解的处理器(AnnotationBeanPostProcessor)和一些事件监听处理器
Set processorDefinitions = AnnotationConfigUtils.registerAnnotationConfigProcessors(parserContext.getRegistry(), source); CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
parserContext.pushContainingComponent(compDefinition); for (BeanDefinitionHolder processorDefinition : processorDefinitions) {
parserContext.registerComponent(new BeanComponentDefinition(processorDefinition));
} parserContext.popAndRegisterContainingComponent(); return null;
}
}

查看registerAnnotationConfigProcessors方法

public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry, Object source) {
DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
if (beanFactory != null) {
if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
beanFactory
.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
}
if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
beanFactory
.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
}
} Set beanDefs = new LinkedHashSet(4); if (!(registry
.containsBeanDefinition("org.springframework.context.annotation.internalConfigurationAnnotationProcessor"))) {
RootBeanDefinition def = new RootBeanDefinition(
ConfigurationClassPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(
registry,
def,
"org.springframework.context.annotation.internalConfigurationAnnotationProcessor"));
}
     //Autowired注解处理器
if (!(registry
.containsBeanDefinition("org.springframework.context.annotation.internalAutowiredAnnotationProcessor"))) {
RootBeanDefinition def = new RootBeanDefinition(
AutowiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def,
"org.springframework.context.annotation.internalAutowiredAnnotationProcessor"));
}
     //Required注解处理器  
if (!(registry
.containsBeanDefinition("org.springframework.context.annotation.internalRequiredAnnotationProcessor"))) {
RootBeanDefinition def = new RootBeanDefinition(
RequiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def,
"org.springframework.context.annotation.internalRequiredAnnotationProcessor"));
}
     //@Resource、@PostConstruct、@PreDestroy等注解处理器
if ((jsr250Present)
&& (!(registry
.containsBeanDefinition("org.springframework.context.annotation.internalCommonAnnotationProcessor")))) {
RootBeanDefinition def = new RootBeanDefinition(
CommonAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def,
"org.springframework.context.annotation.internalCommonAnnotationProcessor"));
} if ((jpaPresent)
&& (!(registry
.containsBeanDefinition("org.springframework.context.annotation.internalPersistenceAnnotationProcessor")))) {
RootBeanDefinition def = new RootBeanDefinition();
try {
def.setBeanClass(ClassUtils
.forName(
"org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor",
AnnotationConfigUtils.class.getClassLoader()));
} catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Cannot load optional framework class: org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor",
ex);
} def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def,
"org.springframework.context.annotation.internalPersistenceAnnotationProcessor"));
} if (!(registry
.containsBeanDefinition("org.springframework.context.event.internalEventListenerProcessor"))) {
RootBeanDefinition def = new RootBeanDefinition(
EventListenerMethodProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def,
"org.springframework.context.event.internalEventListenerProcessor"));
}
if (!(registry
.containsBeanDefinition("org.springframework.context.event.internalEventListenerFactory"))) {
RootBeanDefinition def = new RootBeanDefinition(
DefaultEventListenerFactory.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def,
"org.springframework.context.event.internalEventListenerFactory"));
} return beanDefs;
}

for循环逐个注册实例,registerComponent方法

public void registerComponent(ComponentDefinition component) {
CompositeComponentDefinition containingComponent = getContainingComponent();
if (containingComponent != null) {
containingComponent.addNestedComponent(component);
} else
this.readerContext.fireComponentRegistered(component);
}

addNestedComponent方法(省略部分代码)

public class CompositeComponentDefinition extends AbstractComponentDefinition {

  private final List<ComponentDefinition> nestedComponents = new LinkedList();
  public void addNestedComponent(ComponentDefinition component) {
Assert.notNull(component, "ComponentDefinition must not be null");
this.nestedComponents.add(component);
}
}

可看到,最终实例存到了一个链表中,整个bean注册过程基本完成。

不过,该标签的功能被

<context:component-scan base-package=”**.**”/>

标签所包含,及扫描时也会注入上述实例,所以,在配置了自动扫描包配置之后,本标签可省略不配置。

springMVC之<context:annotation-config />标签的更多相关文章

  1. Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException

    org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean ...

  2. 【原创】大叔经验分享(16)Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    今天尝试运行一个古老的工程,配置好之后编译通过,结果运行时报错: org.springframework.beans.factory.BeanDefinitionStoreException: Une ...

  3. spring 2.5.6 错误:Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    在运行一个第三方公司交付的项目的时候, 出现: Caused by: java.lang.IllegalStateException: Context namespace element 'annot ...

  4. context、config

    Tomcat启动时已经创建了context,并使用它读取了web.xml中的参数,后台可以从context里获取参数 后台获取参数代码: ServletContext context = getSer ...

  5. Context namespace element 'annotation-config' and its parser class [org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser]

    严重: Exception sending context initialized event to listener instance of class org.springframework.we ...

  6. class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    在搭建SSM项目时报了以下的错误: 06-Oct-2019 11:55:52.109 信息 [RMI TCP Connection(5)-127.0.0.1] org.apache.catalina. ...

  7. SpringAOP中的aop:config标签

    我们使用Spring的AOP功能的时候发现,我们使用普通的配置方式的时候,我们无法精确的确定将切面类中的哪个方法切入到哪个切入点上, 所以我们可以使用aop的专用标签来完成相关的配置.其中主要表现是使 ...

  8. [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher 问题--MyEclipse设置JDK版本

    org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML doc ...

  9. Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    异常信息如下: 错误: Unexpected exception parsing XML document from class path resource [spring/applicationCo ...

随机推荐

  1. python基础编程

    1.if else var1 = 100 if var1: print ("1 - if 表达式条件为 true") print (var1) #为0时,条件不成立 var2 = ...

  2. eclipse设置项目编码

    首先Windows->Preferences, 然后选择General下面的Workspace. Text file encoding选择Other GBK, 如果没有GBK的选项, 没关系, ...

  3. WF2013Low Power芯片

    Description     有n个机器,每个机器有2个芯片,每个芯片可以放k个电池.     每个芯片能量是k个电池的能量的最小值.     两个芯片的能量之差越小,这个机器就工作的越好.     ...

  4. [经验] Win7减肥攻略(删文件不删功能、简化优化系统不简优化性能)

    [经验] Win7减肥攻略(删文件不删功能.简化优化系统不简优化性能) ☆心梦无痕☆ 发表于 2014-1-24 11:15:04 https://www.itsk.com/thread-316471 ...

  5. express+gulp构建项目(四)env环境变量

    这里的文件的作用是负责设置env环境变量和日志. index.js try { require('dotenv').load({silent: true}); //dotenv从一个.env文件中读取 ...

  6. JAVA中的正则表达式

    正则表达式:是字符串内容的匹配模板 正则表达式本身就是一个字符串 正则表达式的模糊符号: 一个[ ]代表一个字符,括号里面表示可以选择那些字符 { }用来表示前面这个正则表达式出现的次数 ?+* 这三 ...

  7. JavaScript中的splice方法

    splice方法根据传入的不同参数可分别实现删除和插入操作 使用splice(pra1,pra2,pra3)方法,需要为其提供如下参数: 1.pra1为其起始索引(即希望开始添加元素的地方) 2.pr ...

  8. flume介绍与原理(一)

    1 .背景 flume是由cloudera软件公司产出的可分布式日志收集系统,后与2009年被捐赠了apache软件基金会,为hadoop相关组件之一.尤其近几年随着flume的不断被完善以及升级版本 ...

  9. 判断一个url地址是不是404状态(用curl函数)

    <?php $url = "http://www.kxblogs.com/n/20161108/74429879.html"; $ch = curl_init (); cur ...

  10. 优化Google字体 全面加速WordPress

    从5月27号起,由于某些原因,Google服务在大陆的崩溃影响了数百万的站长,因为很多wordpress主题都在使用Google的在线字体方案-google fonts包括新版的WordPress 后 ...