文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
3.9 Annotation-based container configuration
Are annotations better than XML for configuring Spring?
The introduction of annotation-based configurations raised the question of whether this approach is ‘better’ than XML. The short answer is it depends. The long answer is that each approach has its pros and cons, and usually it is up to the developer to decide which strategy suits them better. Due to the way they are defined, annotations provide a lot of context in their declaration, leading to shorter and more concise configuration. However, XML excels at wiring up components without touching their source code or recompiling them. Some developers prefer having the wiring close to the source while others argue that annotated classes are no longer POJOs and, furthermore, that the configuration becomes decentralized and harder to control.
No matter the choice, Spring can accommodate both styles and even mix them together. It’s worth pointing out that through its JavaConfig option, Spring allows annotations to be used in a non-invasive way, without touching the target components source code and that in terms of tooling, all configuration styles are supported by the Spring Tool Suite.
在配置Spring时注解是否比XML更好?
基于注解配置的引入引出了一个问题——这种方式是否比基于XML的配置更好。简短的回答是视情况而定。长一点的回答是每种方法都有它的优点和缺点,通常是由开发者决定哪一种策略更适合他们。由于注解的定义方式,注解在它们的声明中提供了许多上下文,导致配置更简短更简洁。然而,XML擅长连接组件而不必接触源代码或重新编译它们。一些开发者更喜欢接近源代码,而另一些人则认为基于注解的类不再是POJOs,此外,配置变的去中心化,而且更难控制。
无论选择是什么,Spring都能容纳这两种风格,甚至可以将它们混合在一起。值得指出的是,通过它的Java配置选项,Spring允许注解以一种非入侵的方式使用,不触碰目标组件源码和那些工具,所有的配置风格由Spring工具套件支持。
An alternative to XML setups is provided by annotation-based configuration which rely on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration. As mentioned in the section called “Example: The RequiredAnnotationBeanPostProcessor”, using a BeanPostProcessor
in conjunction with annotations is a common means of extending the Spring IoC container. For example, Spring 2.0 introduced the possibility of enforcing required properties with the @Required
annotation. Spring 2.5 made it possible to follow that same general approach to drive Spring’s dependency injection. Essentially, the @Autowired
annotation provides the same capabilities as described in Section 3.4.5, “Autowiring collaborators” but with more fine-grained control and wider applicability. Spring 2.5 also added support for JSR-250 annotations such as @PostConstruct
, and @PreDestroy
. Spring 3.0 added support for JSR-330 (Dependency Injection for Java) annotations contained in the javax.inject
package such as @Inject
and @Named
. Details about those annotations can be found in the relevant section.
基于注解的配置提供了一种XML设置的可替代方式,它依赖于字节码元数据来组装组件,而不是用尖括号声明的方式。代替使用XML来描述bean组装,开发者通过将注解使用在相关的类,方法或字段声明中,将配置移动到了组件类本身的内部。正如在“Example: The RequiredAnnotationBeanPostProcessor”那节提到的那样,使用BeanPostProcessor
与注解结合是扩展Spring IoC容器的的常见方法。例如,Spring 2.0引入了@Required
注解来执行需要的属性的可能性。Spring 2.5使以同样地通用方法来驱动Spring的依赖注入变为可能。本质上来说,@Autowired
提供了如3.4.5小节描述的同样的能力。“Autowiring collaborators”但更细粒度的控制和更广的应用性。Spring 2.5也添加对JSR-250注解的支持,例如,@PostConstruct
和@PreDestroy
。Spring 3.0添加了对JSR-330,包含在javax.inject
包内的注解(Java的依赖注入)的支持,例如@Inject
和@Named
。关于这些注解的细节可以在相关的小节找到。
Annotation injection is performed before XML injection, thus the latter configuration will override the former for properties wired through both approaches.
注解注入在XML注入之前进行,因此对于通过两种方法进行组装的属性后者的配置会覆盖前者。
As always, you can register them as individual bean definitions, but they can also be implicitly registered by including the following tag in an XML-based Spring configuration (notice the inclusion of the context namespace):
跟以前一样,你可以作为单独的bean定义来注册它们,但也可以通过在一个基于XML的Spring配置(注入包含上下文命名空间)中包含下面的标签来隐式的注册它们:
1 | <?xml version="1.0" encoding="UTF-8"?> |
(The implicitly registered post-processors include AutowiredAnnotationBeanPostProcessor
, CommonAnnotationBeanPostProcessor
, PersistenceAnnotationBeanPostProcessor
, as well as the aforementioned RequiredAnnotationBeanPostProcessor
.)
(隐式注册的后处理器包括 AutowiredAnnotationBeanPostProcessor
,CommonAnnotationBeanPostProcessor
,PersistenceAnnotationBeanPostProcessor
和前面提到的RequiredAnnotationBeanPostProcessor
。)
<context:annotation-config/>
only looks for annotations on beans in the same application context in which it is defined. This means that, if you put<context:annotation-config/>
in aWebApplicationContext
for aDispatcherServlet
, it only checks for@Autowired
beans in your controllers, and not your services. See Section 18.2, “The DispatcherServlet” for more information.
<context:annotation-config/>
仅在定义它的同样的应用上下文中寻找注解的beans。这意味着,如果你在一个为DispatcherServlet
服务的WebApplicationContext
中放置了<context:annotation-config/>
,它只能在你的控制器中寻找@Autowired
注解的beans,而不是在你的服务层中。更多信息请看18.2小节,“The DispatcherServlet”。
3.9.1 @Required
The @Required
annotation applies to bean property setter methods, as in the following example:
@Required
注解应用到bean属性的setter方法上,例子如下:
1 | public class SimpleMovieLister { |
This annotation simply indicates that the affected bean property must be populated at configuration time, through an explicit property value in a bean definition or through autowiring. The container throws an exception if the affected bean property has not been populated; this allows for eager and explicit failure, avoiding NullPointerExceptions
or the like later on. It is still recommended that you put assertions into the bean class itself, for example, into an init method. Doing so enforces those required references and values even when you use the class outside of a container.
这个注解仅仅是表明受影响的bean属性必须在配置时通过显式的bean定义或自动组装填充。如果受影响的bean属性没有填充,容器会抛出一个异常,这允许及早明确的失败,避免NullPointerExceptions
或后面出现类似的情况。仍然建议你在bean类本身加入断言,例如,加入到初始化方法中。这样做可以强制这些需要的引用和值,甚至是你在容器外部使用这个类的时候。
3.9.2 @Autowired
JSR 330’s
@Inject
annotation can be used in place of Spring’s@Autowired
annotation in the examples below. See here for more details.
在下面的例子中JSR 330的
@Inject
注解可以用来代替Spring的@Autowired
注解。
You can apply the @Autowired
annotation to constructors:
你可以将@Autowired
注解应用到构造函数上。
1 | public class MovieRecommender { |
As of Spring Framework 4.3, the
@Autowired
constructor is no longer necessary if the target bean only defines one constructor. If several constructors are available, at least one must be annotated to teach the container which one it has to use.
从Spring框架4.3起,如果目标bena仅定义了一个构造函数,那么
@Autowired
注解的构造函数不再是必要的。如果一些构造函数是可获得的,至少有一个必须要加上注解,以便于告诉容器使用哪一个。
As expected, you can also apply the @Autowired
annotation to “traditional” setter methods:
正如预料的那样,你也可以将@Autowired
注解应用到“传统的”setter方法上:
1 | public class SimpleMovieLister { |
You can also apply the annotation to methods with arbitrary names and/or multiple arguments:
你也可以应用注解到具有任何名字和/或多个参数的方法上:
1 | public class MovieRecommender { |
You can apply @Autowired
to fields as well and even mix it with constructors:
你也可以应用@Autowired
到字段上,甚至可以与构造函数混合用:
1 | public class MovieRecommender { |
It is also possible to provide all beans of a particular type from the ApplicationContext
by adding the annotation to a field or method that expects an array of that type:
通过给带有数组的字段或方法添加@Autowired
注解,也可以从ApplicationContext
中提供一组特定类型的bean:
1 | public class MovieRecommender { |
The same applies for typed collections:
同样也可以应用到具有同一类型的集合上:
1 | public class MovieRecommender { |
Your beans can implement the
org.springframework.core.Ordered
interface or either use the@Order
or standard@Priority
annotation if you want items in the array or list to be sorted into a specific order.
如果你希望数组或列表中的项按指定顺序排序,你的bean可以实现
org.springframework.core.Ordered
接口,或使用@Order
或标准@Priority
注解。
Even typed Maps can be autowired as long as the expected key type is String
. The Map values will contain all beans of the expected type, and the keys will contain the corresponding bean names:
只要期望的key是String
,那么类型化的Maps就可以自动组装。Map的值将包含所有期望类型的beans,key将包含对应的bean名字:
1 | public class MovieRecommender { |
By default, the autowiring fails whenever zero candidate beans are available; the default behavior is to treat annotated methods, constructors, and fields as indicating required dependencies. This behavior can be changed as demonstrated below.
默认情况下,当没有候选beans可获得时,自动组装会失败;默认的行为是将注解的方法,构造函数和字段看作指明了需要的依赖。这个行为也可以通过下面的方式去改变。
1 | public class SimpleMovieLister { |
Only one annotated constructor per-class can be marked as required, but multiple non-required constructors can be annotated. In that case, each is considered among the candidates and Spring uses the greediest constructor whose dependencies can be satisfied, that is the constructor that has the largest number of arguments.
@Autowired
’srequired
attribute is recommended over the@Required
annotation. The required attribute indicates that the property is not required for autowiring purposes, the property is ignored if it cannot be autowired.@Required
, on the other hand, is stronger in that it enforces the property that was set by any means supported by the container. If no value is injected, a corresponding exception is raised.
每个类只有一个构造函数可以标记为必需的,但可以注解多个非必需的构造函数。在这种情况下,会考虑这些候选者中的每一个,Spring使用最贪婪的构造函数,即依赖最满足的构造函数,具有最大数目的参数。
建议在
@Required
注解之上使用@Autowired
的required
特性。required
特性表明这个属性自动装配是不需要的,如果这个属性不能被自动装配,它会被忽略。另一方面@Required
是更强大的,在它强制这个属性被任何容器支持的bean设置。如果没有值注入,会抛出对应的异常。
You can also use @Autowired
for interfaces that are well-known resolvable dependencies: BeanFactory
, ApplicationContext
, Environment
, ResourceLoader
, ApplicationEventPublisher
, and MessageSource
. These interfaces and their extended interfaces, such as ConfigurableApplicationContext
or ResourcePatternResolver
, are automatically resolved, with no special setup necessary.
你也可以对那些已知的具有可解析依赖的接口使用@Autowired
:BeanFactory
,ApplicationContext
,Environment
, ResourceLoader
,ApplicationEventPublisher
和MessageSource
。这些接口和它们的扩展接口,例如ConfigurableApplicationContext
或ResourcePatternResolver
,可以自动解析,不需要特别的设置。
1 | public class MovieRecommender { |
@Autowired
,@Inject
,@Resource
, and@Value
annotations are handled by SpringBeanPostProcessor
implementations which in turn means that you cannot apply these annotations within your ownBeanPostProcessor
orBeanFactoryPostProcessor
types (if any). These types must be ‘wired up’ explicitly via XML or using a Spring@Bean
method.
@Autowired
,@Inject
,@Resource
和@Value
注解是通过SpringBeanPostProcessor
实现处理,这反过来意味着你不能在你自己的BeanPostProcessor
或BeanFactoryPostProcessor
中应用这些注解(如果有的话)。这些类型必须显式的通过XML或使用Spring的@Bean
方法来’wired up’。
3.9.3 Fine-tuning annotation-based autowiring with @Primary
Because autowiring by type may lead to multiple candidates, it is often necessary to have more control over the selection process. One way to accomplish this is with Spring’s @Primary
annotation. @Primary
indicates that a particular bean should be given preference when multiple beans are candidates to be autowired to a single-valued dependency. If exactly one ‘primary’ bean exists among the candidates, it will be the autowired value.
因为根据类型的自动装配可能会导致多个候选目标,所以在选择过程中进行更多的控制经常是有必要的。一种方式通过Spring的@Primary
注解来完成。当有个多个候选bean要组装到一个单值的依赖时,@Primary
表明指定的bean应该具有更高的优先级。如果确定一个’primary’ bean位于候选目标中间,它将是那个自动装配的值。
Let’s assume we have the following configuration that defines firstMovieCatalog
as the primary MovieCatalog
.
假设我们具有如下配置,将firstMovieCatalog
定义为主要的MovieCatalog
。
1 |
|
With such configuration, the following MovieRecommender
will be autowired with the firstMovieCatalog
.
根据这样的配置,下面的MovieRecommender
将用firstMovieCatalog
进行自动装配。
1 | public class MovieRecommender { |
The corresponding bean definitions appear as follows.
对应的bean定义如下:
1 | <?xml version="1.0" encoding="UTF-8"?> |
3.9.4 Fine-tuning annotation-based autowiring with qualifiers
@Primary
is an effective way to use autowiring by type with several instances when one primary candidate can be determined. When more control over the selection process is required, Spring’s @Qualifier
annotation can be used. You can associate qualifier values with specific arguments, narrowing the set of type matches so that a specific bean is chosen for each argument. In the simplest case, this can be a plain descriptive value:
当有多个实例需要确定一个主要的候选对象时,@Primary
是一种按类型自动装配的有效方式。当需要在选择过程中进行更多的控制时,可以使用Spring的@Qualifier
注解。为了给每个选择一个特定的bean,你可以将限定符的值与特定的参数联系在一起,减少类型匹配集合。在最简单的情况下,这是一个纯描述性值:
1 | public class MovieRecommender { |
The @Qualifier
annotation can also be specified on individual constructor arguments or method parameters:
@Qualifier
注解也可以指定单个构造函数参数或方法参数:
1 | public class MovieRecommender { |
The corresponding bean definitions appear as follows. The bean with qualifier value “main” is wired with the constructor argument that is qualified with the same value.
对应的bean定义如下。限定符值为”main”的bean被组装到有相同值的构造函数参数中。
1 | <?xml version="1.0" encoding="UTF-8"?> |
For a fallback match, the bean name is considered a default qualifier value. Thus you can define the bean with an id “main” instead of the nested qualifier element, leading to the same matching result. However, although you can use this convention to refer to specific beans by name, @Autowired
is fundamentally about type-driven injection with optional semantic qualifiers. This means that qualifier values, even with the bean name fallback, always have narrowing semantics within the set of type matches; they do not semantically express a reference to a unique bean id. Good qualifier values are “main” or “EMEA” or “persistent”, expressing characteristics of a specific component that are independent from the bean id
, which may be auto-generated in case of an anonymous bean definition like the one in the preceding example.
对于回退匹配,bean名字被认为是默认的限定符值。因此你可以定义一个id为main
的bean来代替内嵌的限定符元素,会有同样的匹配结果。然而,尽管你可以使用这个约定根据名字引用特定的beans,但是@Autowired
从根本上来讲是使用可选的语义限定符来进行类型驱动注入的。这意味着限定符的值,即使回退到bean名称,总是缩小语义类型匹配的集合;它们没有从语义上将一个引用表达为一个唯一的bean id。好的限定符值是”main”或”EMEA”或”persistent”,表达一个特定组件的性质,这个组件是独立于bean id
的,即使前面例子中像这个bean一样的匿名bean会自动生成id。
Qualifiers also apply to typed collections, as discussed above, for example, to Set<MovieCatalog>
. In this case, all matching beans according to the declared qualifiers are injected as a collection. This implies that qualifiers do not have to be unique; they rather simply constitute filtering criteria. For example, you can define multiple MovieCatalog
beans with the same qualifier value “action”, all of which would be injected into a Set<MovieCatalog>
annotated with @Qualifier("action")
.
正如前面讨论的那样,限定符也可以应用到类型结合上,例如,Set<MovieCatalog>
。在这个例子中,根据声明的限定符匹配的所有beans作为一个集合进行注入。这意味着限定符不必是唯一的;它们只是构成过滤标准。例如,你可以定义多个具有同样限定符值”action”的MovieCatalog
,所有的这些都将注入到带有注解@Qualifier("action")
的Set<MovieCatalog>
中。
If you intend to express annotation-driven injection by name, do not primarily use
@Autowired
, even if is technically capable of referring to a bean name through@Qualifier
values. Instead, use the JSR-250@Resource
annotation, which is semantically defined to identify a specific target component by its unique name, with the declared type being irrelevant for the matching process.@Autowired
has rather different semantics: After selecting candidate beans by type, the specifiedString
qualifier value will be considered within those type-selected candidates only, e.g. matching an “account” qualifier against beans marked with the same qualifier label.For beans that are themselves defined as a collection/map or array type,
@Resource
is a fine solution, referring to the specific collection or array bean by unique name. That said, as of 4.3, collection/map and array types can be matched through Spring’s@Autowired
type matching algorithm as well, as long as the element type information is preserved in@Bean
return type signatures or collection inheritance hierarchies. In this case, qualifier values can be used to select among same-typed collections, as outlined in the previous paragraph.As of 4.3,
@Autowired
also considers self references for injection, i.e. references back to the bean that is currently injected. Note that self injection is a fallback; regular dependencies on other components always have precedence. In that sense, self references do not participate in regular candidate selection and are therefore in particular never primary; on the contrary, they always end up as lowest precedence. In practice, use self references as a last resort only, e.g. for calling other methods on the same instance through the bean’s transactional proxy: Consider factoring out the affected methods to a separate delegate bean in such a scenario. Alternatively, use@Resource
which may obtain a proxy back to the current bean by its unique name.
@Autowired
applies to fields, constructors, and multi-argument methods, allowing for narrowing through qualifier annotations at the parameter level. By contrast,@Resource
is supported only for fields and bean property setter methods with a single argument. As a consequence, stick with qualifiers if your injection target is a constructor or a multi-argument method.
@Autowired
可以应用到字段,构造函数和多参数方法上,允许通过限定符注解在参数层面上缩减候选目标。相比之下,@Resource
仅支持字段和bean属性的带有单个参数的setter方法。因此,如果你的注入目标是一个构造函数或一个多参数的方法,坚持使用限定符。
如果你想通过名字表达注解驱动的注入,不要主要使用
@Autowired
,虽然在技术上能通过@Qualifier
值引用一个bean名字。作为可替代产品,可以使用JSR-250@Resource
注解,它在语义上被定义为通过组件唯一的名字来识别特定的目标组件,声明的类型与匹配过程无关。@Autowired
有不同的语义:通过类型选择候选beans,特定的String
限定符值被认为只在类型选择的候选目标中,例如,在那些标记为具有相同限定符标签的beans中匹配一个”account”限定符。对于那些本身定义在集合/映射或数组类型中的beans来说,
@Resource
是一个很好的解决方案,适用于特定的集合或通过唯一名字区分的数组bean。也就是说,自Spring 4.3起,集合/映射和数组类型中也可以通过Spring的@Autowired
类型匹配算法进行匹配,只要元素类型信息在@Bean
中保留,返回类型签名或集合继承体系。在这种情况下,限定符值可以用来在相同类型的集合中选择,正如在前一段中概括的那样。自Spring 4.3起,
@Autowired
也考虑自引用注入,例如,引用返回当前注入的bean。注意自注入是备用;普通对其它组件的依赖关系总是优先的。在这个意义上,自引用不参与普通的候选目标选择,因此尤其是从不是主要的;恰恰相反,它们最终总是最低的优先级。在实践中,自引用只是作为最后的手段,例如,通过bean的事务代理调用同一实例的其它方法:在考虑抽出受影响的方法来分隔代理bean的场景中。或者,使用@Resource
通过它的唯一名字可能得到一个返回当前bean的代理。
@Autowired
可以应用到字段,构造函数和多参数方法上,允许通过限定符注解在参数层面上缩减候选目标。相比之下,@Resource
仅支持字段和bean属性的带有单个参数的setter方法。因此,如果你的注入目标是一个构造函数或一个多参数的方法,坚持使用限定符。
You can create your own custom qualifier annotations. Simply define an annotation and provide the @Qualifier
annotation within your definition:
你可以创建自己的定制限定符注解。简单定义一个注解,在你自己的定义中提供@Qualifier
注解:
1 | ({ElementType.FIELD, ElementType.PARAMETER}) |
Then you can provide the custom qualifier on autowired fields and parameters:
然后你可以在自动装配的字段和参数上提供定制的限定符:
1 | public class MovieRecommender { |
Next, provide the information for the candidate bean definitions. You can add <qualifier/>
tags as sub-elements of the <bean/>
tag and then specify the type and value to match your custom qualifier annotations. The type is matched against the fully-qualified class name of the annotation. Or, as a convenience if no risk of conflicting names exists, you can use the short class name. Both approaches are demonstrated in the following example.
接下来,提供候选bean定义的信息。你可以添加<qualifier/>
标记作为<bean/>
标记的子元素,然后指定匹配你的定制限定符注解的类型和值。类型用来匹配注解的全限定类名称。或者,如果没有名称冲突的风险,为了方便,你可以使用简写的类名称。下面的例子证实了这些方法。
1 | <?xml version="1.0" encoding="UTF-8"?> |
In Section 3.10, “Classpath scanning and managed components”, you will see an annotation-based alternative to providing the qualifier metadata in XML. Specifically, see Section 3.10.8, “Providing qualifier metadata with annotations”.
在3.10小节,“类路径扫描和管理组件”中,你将看到一个基于注解的替代方法,在XML中提供限定符元数据。特别地,看3.10.8小节,“用注解提供限定符元数据”。
In some cases, it may be sufficient to use an annotation without a value. This may be useful when the annotation serves a more generic purpose and can be applied across several different types of dependencies. For example, you may provide an offline catalog that would be searched when no Internet connection is available. First define the simple annotation:
在某些情况下,使用没有值的注解就是足够的。当注解为了通用的目的时,这是非常有用的,可以应用到跨几个不同类型的依赖上。例如,当网络不可用时,你可以提供一个要搜索的离线目录。首先定义一个简单的注解:
1 | ({ElementType.FIELD, ElementType.PARAMETER}) |
Then add the annotation to the field or property to be autowired:
然后将注解添加到要自动装配的字段或属性上:
1 | public class MovieRecommender { |
Now the bean definition only needs a qualifier type:
现在bean定义只需要一个限定符类型:
1 | <bean class="example.SimpleMovieCatalog"> |
You can also define custom qualifier annotations that accept named attributes in addition to or instead of the simple value attribute. If multiple attribute values are then specified on a field or parameter to be autowired, a bean definition must match all such attribute values to be considered an autowire candidate. As an example, consider the following annotation definition:
你也可以定义接收命名属性之外的定制限定符注解或代替简单的值属性。如果要注入的字段或参数指定了多个属性值,bean定义必须匹配所有的属性值才会被认为是一个可自动装配的候选目标。作为一个例子,考虑下面的注解定义:
1 | @Target({ElementType.FIELD, ElementType.PARAMETER}) |
In this case Format
is an enum:
这种情况下Format
是枚举类型:
1 | public enum Format { |
The fields to be autowired are annotated with the custom qualifier and include values for both attributes: genre
and format
.
要自动装配的字段使用定制限定符进行注解,并且包含了两个属性值:genre
和format
。
1 | public class MovieRecommender { |
Finally, the bean definitions should contain matching qualifier values. This example also demonstrates that bean meta attributes may be used instead of the <qualifier/>
sub-elements. If available, the <qualifier/>
and its attributes take precedence, but the autowiring mechanism falls back on the values provided within the <meta/>
tags if no such qualifier is present, as in the last two bean definitions in the following example.
最后,bean定义应该包含匹配的限定符值。这个例子也证实了bean元属性可以用来代替<qualifier/>
子元素。如果可获得<qualifier/>
,它和它的属性优先级更高,如果当前没有限定符,自动装配机制会将<meta/>
内的值作为备用,正如下面的例子中的最后两个bean定义。
1 | <?xml version="1.0" encoding="UTF-8"?> |
3.9.5 Using generics as autowiring qualifiers
In addition to the @Qualifier
annotation, it is also possible to use Java generic types as an implicit form of qualification. For example, suppose you have the following configuration:
除了@Qualifier
注解外,也可以使用Java的泛型类型作为限定符的一种隐式方式。例如,假设你有如下配置:
1 | @Configuration |
Assuming that beans above implement a generic interface, i.e. Store<String>
and Store<Integer>
, you can @Autowire
the Store
interface and the generic will be used as a qualifier:
假设上面的beans实现了一个泛型接口,例如,Store<String>
和Store<Integer>
,你可以@Autowire
Store
接口,泛型将作为限定符使用:
1 |
|
Generic qualifiers also apply when autowiring Lists
, Maps
and Arrays
:
当自动装配Lists
,Maps
和Arrays
时,也会应用泛型限定符:
1 | // Inject all Store beans as long as they have an <Integer> generic |
3.9.6 CustomAutowireConfigurer
The CustomAutowireConfigurer
is a BeanFactoryPostProcessor
that enables you to register your own custom qualifier annotation types even if they are not annotated with Spring’s @Qualifier
annotation.
CustomAutowireConfigurer
是一个能使你注册自己的定制限定符注解类型的BeanFactoryPostProcessor
,即使它们不能使用Spring的@Qualifier
注解进行注解。
1 | <bean id="customAutowireConfigurer" |
The AutowireCandidateResolver
determines autowire candidates by:
the
autowire-candidate
value of each bean definitionany
default-autowire-candidates
pattern(s) available on the<beans/>
elementthe presence of
@Qualifier
annotations and any custom annotations registered with theCustomAutowireConfigurer
AutowireCandidateResolver
通过下面的方式决定自动装配的候选目标:
每个bean定义的
autowire-candidate
在
<beans/>
元素可获得的任何default-autowire-candidates
模式存在
@Qualifier
注解和任何在CustomAutowireConfigurer
中注册的定制注解
When multiple beans qualify as autowire candidates, the determination of a “primary” is the following: if exactly one bean definition among the candidates has a primary
attribute set to true
, it will be selected.
当多个beans符合条件成为自动装配的候选目标时,”primary” bean的决定如下:如果在候选目标中某个确定的bean中的primary
特性被设为true
,它将被选为目标bean。
3.9.7 @Resource
Spring also supports injection using the JSR-250 @Resource
annotation on fields or bean property setter methods. This is a common pattern in Java EE 5 and 6, for example in JSF 1.2 managed beans or JAX-WS 2.0 endpoints. Spring supports this pattern for Spring-managed objects as well.
Spring也支持使用JSR-250 @Resource
对字段或bean属性setter方法进行注入。这是在Java EE 5和6中的一种通用模式,例如在JSF 1.2管理的beans或JAX-WS 2.0的端点。Spring对它管理的对象也支持这种模式。
@Resource
takes a name attribute, and by default Spring interprets that value as the bean name to be injected. In other words, it follows by-name semantics, as demonstrated in this example:
@Resource
采用名字属性,默认情况下Spring将名字值作为要注入的bean的名字。换句话说,它遵循by-name语义,下面的例子证实了这一点:
1 | public class SimpleMovieLister { |
If no name is specified explicitly, the default name is derived from the field name or setter method. In case of a field, it takes the field name; in case of a setter method, it takes the bean property name. So the following example is going to have the bean with name “movieFinder” injected into its setter method:
如果没有显式的指定名字,默认名字从字段名或setter方法中取得。在字段情况下,它采用字段名称;在setter方法情况下,它采用bean的属性名。因此下面的例子将名字为movieFinder
的bean注入到它的setter方法中:
1 | public class SimpleMovieLister { |
The name provided with the annotation is resolved as a bean name by the
ApplicationContext
of which theCommonAnnotationBeanPostProcessor
is aware. The names can be resolved through JNDI if you configure Spring’sSimpleJndiBeanFactory
explicitly. However, it is recommended that you rely on the default behavior and simply use Spring’s JNDI lookup capabilities to preserve the level of indirection.
注解提供的名字被
CommonAnnotationBeanPostProcessor
感知的ApplicationContext
解析为bean名字。如果你显式地配置了Spring的SimpleJndiBeanFactory
,名字会通过JNDI解析。但是建议你依赖默认行为,简单使用Spring的JNDI查找功能保护间接查找级别。
In the exclusive case of @Resource
usage with no explicit name specified, and similar to @Autowired
, @Resource
finds a primary type match instead of a specific named bean and resolves well-known resolvable dependencies: the BeanFactory
, ApplicationContext
, ResourceLoader
, ApplicationEventPublisher
, and MessageSource
interfaces.
在@Resource
特有的没有显式名字指定的情况下,类似于@Autowired
,@Resource
会进行主要的匹配类型来代替指定名字的bean并解析已知的可解析依赖:BeanFactory
,ApplicationContext
,ResourceLoader
,ApplicationEventPublisher
和MessageSource
接口。
Thus in the following example, the customerPreferenceDao
field first looks for a bean named customerPreferenceDao
, then falls back to a primary type match for the type CustomerPreferenceDao
. The “context” field is injected based on the known resolvable dependency type ApplicationContext
.
因此在下面的例子中,customerPreferenceDao
字段首先查找名字为customerPreferenceDao
的bean,然后回退到主要的类型为CustomerPreferenceDao
的类型匹配。”context”字段会注入基于已知的可解析依赖类型ApplicationContext
。
1 | public class MovieRecommender { |
3.9.8 @PostConstruct and @PreDestroy
The CommonAnnotationBeanPostProcessor
not only recognizes the @Resource
annotation but also the JSR-250 lifecycle annotations. Introduced in Spring 2.5, the support for these annotations offers yet another alternative to those described in initialization callbacks and destruction callbacks. Provided that the CommonAnnotationBeanPostProcessor
is registered within the Spring ApplicationContext
, a method carrying one of these annotations is invoked at the same point in the lifecycle as the corresponding Spring lifecycle interface method or explicitly declared callback method. In the example below, the cache will be pre-populated upon initialization and cleared upon destruction.
CommonAnnotationBeanPostProcessor
不仅识别@Resource
注解,而且识别JSR-250生命周期注解。在Spring 2.5引入了对这些注解的支持,也提供了在初始化回调函数和销毁回调函数中描述的那些注解的一种可替代方式。假设CommonAnnotationBeanPostProcessor
在Spring的ApplicationContext
中注册,执行这些注解的方法在生命周期的同一点被调用,作为对应的Spring生命周期接口方法或显式声明的回调方法。在下面的例子中,缓存会预先放置接近初始化之前,并在销毁之前清除。
1 | public class CachingMovieLister { |
For details about the effects of combining various lifecycle mechanisms, see the section called “Combining lifecycle mechanisms”.
关于组合各种生命周期机制的影响的更多细节,请看“组合生命周期机制”小节。