springMVC的配置文件中常常見到<context:annotation-config/>,那麼這句話的做用究竟是什麼呢?spring
如今的註解很是方便,可是系統如何才能識別註解呢,這就須要相應的處理程序了,springMVC使用AnnotationBeanPostProcessor讓系統可以識別相應的註解ui
例如:若是你想使用Autowired註解,那麼必須先在spring容器中聲明AutoWiredAnnotationBeanPostProccessor的實例,如要使用@Required註解就必須先聲明RequiredAnnotationBeanPostProccessor,通常的方法是在配置文件挨個定義:this
//@AutoWired註解處理器
<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>
//@Required註解處理器
<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
然而對於這些常見的註解,每次顯示定義很麻煩,因此springMVC給咱們提供了隱式定義的方法,即<context:annotation-config/>標籤。spa
下面看看<context:annotation-config/>標籤註冊過程及註冊了哪些實例到容器。3d
首先找到實例定義接口BeanDefinitionParsercode
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方法component
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方法orm
public void registerComponent(ComponentDefinition component) { CompositeComponentDefinition containingComponent = getContainingComponent(); if (containingComponent != null) { containingComponent.addNestedComponent(component); } else
this.readerContext.fireComponentRegistered(component); }
addNestedComponent方法(省略部分代碼)blog
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=」**.**」/>
標籤所包含,及掃描時也會注入上述實例,因此,在配置了自動掃描包配置以後,本標籤可省略不配置。