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

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

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

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


当前回答

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和自动装配的尝试。

其他回答

<上下文:annotation-config >:

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

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

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

<context:component-scan/>自定义标记除了主要负责扫描java包和从类路径注册bean定义外,还注册了与by所做的相同的bean定义集。

如果出于某种原因要避免这种默认bean定义的注册,方法是在component-scan中指定一个额外的“annotation-config”属性,如下所示:

<context:component-scan basePackages="" annotation-config="false"/>

参考: http://www.java-allandsundry.com/2012/12/contextcomponent-scan-contextannotation.html

您可以在spring上下文模式文件中找到更多信息。 以下内容来自spring-context-4.3.xsd

<conxtext:annotation-config />
Activates various annotations to be detected in bean classes: Spring's @Required and
@Autowired, as well as JSR 250's @PostConstruct, @PreDestroy and @Resource (if available),
JAX-WS's @WebServiceRef (if available), EJB 3's @EJB (if available), and JPA's
@PersistenceContext and @PersistenceUnit (if available). Alternatively, you may
choose to activate the individual BeanPostProcessors for those annotations.

Note: This tag does not activate processing of Spring's @Transactional or EJB 3's
@TransactionAttribute annotation. Consider the use of the <tx:annotation-driven>
tag for that purpose.
<context:component-scan>
Scans the classpath for annotated components that will be auto-registered as
Spring beans. By default, the Spring-provided @Component, @Repository, @Service, @Controller, @RestController, @ControllerAdvice, and @Configuration stereotypes    will be detected.

Note: This tag implies the effects of the 'annotation-config' tag, activating @Required,
@Autowired, @PostConstruct, @PreDestroy, @Resource, @PersistenceContext and @PersistenceUnit
annotations in the component classes, which is usually desired for autodetected components
(without external configuration). Turn off the 'annotation-config' attribute to deactivate
this default behavior, for example in order to use custom BeanPostProcessor definitions
for handling those annotations.

Note: You may use placeholders in package paths, but only resolved against system
properties (analogous to resource paths). A component scan results in new bean definitions
being registered; Spring's PropertySourcesPlaceholderConfigurer will apply to those bean
definitions just like to regular bean definitions, but it won't apply to the component
scan settings themselves.

<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迁移到注释

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

>用于不使用XML定义bean

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

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