我正在学习Spring 3,我似乎没有掌握<context:annotation-config>和<context:component-scan>背后的功能。

从我读到的内容来看,它们似乎处理不同的注释(@Required, @Autowired等vs @Component, @Repository, @Service等),但从我读到的内容来看,它们注册了相同的bean后处理器类。

更让我困惑的是,在<context:component-scan>上有一个annotation-config属性。

有人能解释一下这些标签吗?什么是相似的,什么是不同的,是一个被另一个取代,它们互相完善,我需要其中一个吗,还是两个?


当前回答

<context:annotation-config/> <!-- is used to activate the annotation for beans -->
<context:component-scan base-package="x.y.MyClass" /> <!-- is for the Spring IOC container to look for the beans in the base package. -->

要注意的另一个重要点是context:component-scan隐式调用context:annotation-config来激活bean上的注释。如果你不想让context:component-scan隐式地为你激活注释,你可以继续将context:component-scan的annotation-config元素设置为false。

总结:

<context:annotation-config/> <!-- activates the annotations --> 
<context:component-scan base-package="x.y.MyClass" /> <!-- activates the annotations + register the beans by looking inside the base-package -->

其他回答

Spring允许您做两件事:

自动装配bean 自动发现bean

1. Autowiring Usually in applicationContext.xml you define beans and other beans are wired using constructor or setter methods. You can wire beans using XML or annotations. In case you use annotations, you need to activate annotations and you have to add <context:annotation-config /> in applicationContext.xml. This will simplify the structure of the tag from applicationContext.xml, because you will not have to manually wire beans (constructor or setter). You can use @Autowire annotation and the beans will be wired by type.

避免手动XML配置的一个步骤是

2. Autodiscovery Autodiscovery is simplifying the XML one step further, in the sense that you don't even need too add the <bean> tag in applicationContext.xml. You just mark the specific beans with one of the following annotation and Spring will automatically wire the marked beans and their dependencies into the Spring container. The annotations are as follow: @Controller, @Service, @Component, @Repository. By using <context:component-scan> and pointing the base package, Spring will auto-discover and wire the components into Spring container.


作为结论:

<context:annotation-config />被使用是为了能够使用 @ autowired注解 <context:component-scan />用于确定搜索 特定的bean和自动装配的尝试。

<context:component-scan /> implicitly enables <context:annotation-config/>

try with <context:component-scan base-package="…" annotation-config="false"/>,在你的配置中@Service, @Repository, @Component工作正常,但是@Autowired,@Resource和@Inject不工作。

这意味着AutowiredAnnotationBeanPostProcessor将不会被启用,Spring容器将不会处理自动装配注释。

<context:annotation-config>标记告诉Spring扫描代码库,以自动解决包含@Autowired注释的类的依赖关系需求。

Spring 2.5还增加了对JSR-250注释的支持,比如@Resource、@PostConstruct和@PreDestroy。使用这些注释还要求在Spring容器中注册某些beanpostprocessor。与往常一样,这些可以注册为单独的bean定义,但也可以通过在spring配置中包含<context:annotation-config>标记来隐式注册。

摘自Spring文档中的基于注释的配置


Spring提供了自动检测“定型”类的功能,并在ApplicationContext中注册相应的BeanDefinitions。

根据org.springframework.stereotype的javadoc:

原型是表示类型或方法在整个体系结构中的角色的注释(在概念级别,而不是实现级别)。 例如:@控制器@服务@存储库等。 它们是供工具和方面使用的(成为切入点的理想目标)。

要自动检测这样的“原型”类,需要<context:component-scan>标记。

<context:component-scan>标记还告诉Spring在指定的包(及其所有子包)下扫描可注入bean的代码。

两者之间的区别真的很简单!

<context:annotation-config /> 

允许您使用仅限于连接bean的属性和构造函数的注释!

<context:component-scan base-package="org.package"/> 

启用<context:annotation-config />可以做的所有事情,添加使用原型,例如..@组件,@服务,@存储库。因此,您可以连接整个bean,而不仅仅局限于构造函数或属性!

>激活bean中许多不同的注释,无论它们是用XML定义的还是通过组件扫描定义的。

>用于不使用XML定义bean

欲了解更多信息,请阅读:

3.9. 基于注释的容器配置 3.10. 类路径扫描和托管组件