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

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

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

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


当前回答

<context:annotation-config>:扫描并激活spring config xml中已注册bean的注释。

<context:component-scan>: Bean注册+ <context:annotation-config>


@Autowired和@Required是目标属性级别,所以bean应该在使用这些注释之前在spring IOC中注册。要启用这些注释,要么必须注册各自的bean,要么包含<context:annotation-config />。例如,<context:annotation-config />仅适用于已注册的bean。

@Required启用RequiredAnnotationBeanPostProcessor处理工具 @Autowired启用AutowiredAnnotationBeanPostProcessor处理工具

注:注释本身没什么事,我们需要一个Processing Tool,它是底层的一个类,负责核心流程。


@Repository, @Service和@Controller是@Component,它们的目标是类级别。

<context:component-scan>它扫描包并找到并注册bean,它包括<context:annotation-config />所做的工作。

将XML迁移到注释

其他回答

<context:annotation-config>

只解决@Autowired和@Qualifer注释,这就是所有的,它关于依赖注入,还有其他注释做同样的工作,我认为如何@Inject,但都是关于通过注释解决DI。

注意,即使声明了<context:annotation-config>元素,也必须声明类是Bean,记住我们有三个可用选项

XML: <憨豆先生> @注解:@组件,@服务,@存储库,@控制器 JavaConfig: @Configuration, @Bean

现在有了

<context:component-scan>

它做了两件事:

它扫描带有注释的所有类 @组件、@服务、@存储库、@控制器和@配置并创建一个Bean 它与<context:annotation-config>所做的工作相同。

因此,如果你声明<context:component-scan>,就没有必要再声明<context:annotation-config>了。

Thats所有

例如,一个常见的场景是仅通过XML声明bean,并通过注释解析DI

<bean id="serviceBeanA" class="com.something.CarServiceImpl" />
<bean id="serviceBeanB" class="com.something.PersonServiceImpl" />
<bean id="repositoryBeanA" class="com.something.CarRepository" />
<bean id="repositoryBeanB" class="com.something.PersonRepository" />

我们只声明了bean,没有关于<constructor-arg>和<property>, DI是通过@Autowired在它们自己的类中配置的。这意味着服务对它们的存储库组件使用@Autowired,而存储库对JdbcTemplate、DataSource等组件使用@Autowired

我发现了一个很好的总结,说明哪些注释被哪些声明拾取。通过研究它,你会发现<context:component-scan/>识别了<context:annotation-config/>识别的注释的超集,即:

@组件,@服务,@存储库,@控制器,@端点 @Configuration, @Bean, @Lazy, @Scope, @Order, @Primary, @Profile, @DependsOn, @Import, @ImportResource

正如您所看到的,<context:component-scan/>逻辑上扩展了<context:annotation-config/>,具有CLASSPATH组件扫描和Java @Configuration特性。

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

<context:annotation-config /> 

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

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

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

作为补充,您可以使用@ComponentScan以注释的方式使用<context:component-scan>。

在spring.io中也有描述

配置使用with的组件扫描指令 @ configuration类。提供与Spring XML并行的支持 元素。

需要注意的一点是,如果您正在使用SpringBoot, @Configuration和@ComponentScan可以通过使用@SpringBootApplication注释来暗示。

<上下文:annotation-config >:

这告诉Spring,我将使用Annotated bean作为Spring bean,这些将通过@Autowired annotation进行连接,而不是在Spring配置xml文件中声明。

<上下文:component-scan基础包= " com.test……“>:

这告诉Spring容器从哪里开始搜索那些带注释的bean。在这里,spring将搜索基本包的所有子包。