1.4.5. Autowiring collaborators
1.4.5. 自动装配合作者
The Spring container can autowire relationships between collaborating beans. You can allow Spring to resolve collaborators (other beans) automatically for your bean by inspecting the contents of the ApplicationContext. Autowiring has the following advantages: Spring容器可以自动连接合作bean之间的关系。您可以允许Spring通过检查ApplicationContext的内容来自动为您的bean解析协作者(其他bean)。自动装配具有以下优点:
- Autowiring can significantly reduce the need to specify properties or constructor arguments. (Other mechanisms such as a bean template discussed elsewhere in this chapter are also valuable in this regard.)
- 自动装配可以减少指定属性或构造函数参数的需要。(其他机制,例如本章其他地方讨论的bean模板,在这方面也很有价值。)
- Autowiring can update a configuration as your objects evolve. For example, if you need to add a dependency to a class, that dependency can be satisfied automatically without you needing to modify the configuration. Thus autowiring can be especially useful during development, without negating the option of switching to explicit wiring when the code base becomes more stable.
- 随着对象的发展,自动装配可以更新配置。例如,如果需要向类中添加依赖项,则可以自动满足该依赖项,而无需修改配置。因此,自动装配在开发过程中可能特别有用,而且不会影响代码库变得更加稳定时切换到显式布线的选项。
When using XML-based configuration metadata [2], you specify autowire mode for a bean definition with the autowireattribute of the element. The autowiring functionality has four modes. You specify autowiring per bean and thus can choose which ones to autowire. 当使用基于XML的配置元数据[2]时,可以使用元素的自动装配属性为bean定义指定自动装配模式。自动装配功能有四种模式。您可以指定每个bean的自动装配,因此可以选择哪些自动装配。
Table 2. Autowiring Table 2. 自动装配
Mode | Explanation |
---|
no | (Default) No autowiring. Bean references must be defined via a ref element. Changing the default setting is not recommended for larger deployments, because specifying collaborators explicitly gives greater control and clarity. To some extent, it documents the structure of a system. (默认)无自动装配。 必须通过ref元素定义Bean引用。 不建议对较大的部署更改默认设置,因为明确指定协作者可以提供更好的控制和清晰度。 在某种程度上,它记录了系统的结构。 | byName | Autowiring by property name. Spring looks for a bean with the same name as the property that needs to be autowired. For example, if a bean definition is set to autowire by name, and it contains a master property (that is, it has asetMaster(…) method), Spring looks for a bean definition named master, and uses it to set the property. 按属性名称自动装配。 Spring查找与需要自动装配的属性同名的bean。 例如,如果bean定义按名称设置为autowire,并且它包含master属性(即,它具有一个setMaster(…)方法),则Spring会查找名为master的bean定义,并使用它来设置属性。 | byType | Allows a property to be autowired if exactly one bean of the property type exists in the container. If more than one exists, a fatal exception is thrown, which indicates that you may not use byType autowiring for that bean. If there are no matching beans, nothing happens; the property is not set. 如果容器中只存在一个属性类型的bean,则允许自动装配属性。 如果存在多个,则抛出致命异常,这表示您不能对该bean使用byType自动装配。 如果没有匹配的bean,则没有任何反应; 该属性未设定。 | constructor | Analogous to byType, but applies to constructor arguments. If there is not exactly one bean of the constructor argument type in the container, a fatal error is raised. 类似于byType,但适用于构造函数参数。 如果容器中没有构造函数参数类型的一个bean,则会引发致命错误。 |
With byType or constructor autowiring mode, you can wire arrays and typed-collections. In such cases all autowire candidates within the container that match the expected type are provided to satisfy the dependency. You can autowire strongly-typed Maps if the expected key type is String. An autowired Maps values will consist of all bean instances that match the expected type, and the Maps keys will contain the corresponding bean names. 使用byType或构造函数自动装配模式,您可以连线阵列和类型集合。在这种情况下,容器中的所有符合期望类型的自动装配候选都被提供来满足依赖关系。如果预期的键类型是字符串,则可以自动装入强类型映射。自动装配的Maps值将由所有与预期类型匹配的bean实例组成,Maps键将包含相应的bean名称。
You can combine autowire behavior with dependency checking, which is performed after autowiring completes. 您可以将autowire行为与依赖性检查结合起来,这是在自动装配完成后执行的。
Limitations and disadvantages of autowiring 自动装配的局限和缺点 Autowiring works best when it is used consistently across a project. If autowiring is not used in general, it might be confusing to developers to use it to wire only one or two bean definitions. 自动装配在项目中一致使用时效果最佳。如果一般不使用自动装配,开发人员可能会使用它来仅连接一个或两个bean定义。
Consider the limitations and disadvantages of autowiring: 考虑自动装配的局限性和缺点:
- Explicit dependencies in property and constructor-arg settings always override autowiring. You cannot autowire so-called simple properties such as primitives, Strings, and Classes (and arrays of such simple properties). This limitation is by-design.
- 属性和构造函数参数中的显式依赖关系始终会覆盖自动装配。您不能自动调用所谓的简单属性,如基元,字符串和类(以及这些简单属性的数组)。这个限制是通过设计。
- Autowiring is less exact than explicit wiring. Although, as noted in the above table, Spring is careful to avoid guessing in case of ambiguity that might have unexpected results, the relationships between your Spring-managed objects are no longer documented explicitly.
- 自动装配不如准确装配。虽然,如上表所述,Spring在注意避免猜测可能会有意想不到的结果的情况下进行猜测,但您的Spring管理的对象之间的关系不再明确记录。
- Wiring information may not be available to tools that may generate documentation from a Spring container.
- 装配信息可能无法用于可能从Spring容器生成文档的工具。
- Multiple bean definitions within the container may match the type specified by the setter method or constructor argument to be autowired. For arrays, collections, or Maps, this is not necessarily a problem. However for dependencies that expect a single value, this ambiguity is not arbitrarily resolved. If no unique bean definition is available, an exception is thrown.
- 容器中的多个bean定义可以匹配由setter方法或构造函数参数指定的类型以进行自动装配。对于数组,集合或地图,这不一定是个问题。然而,对于期望单一值的依赖关系,这种不明确性不是任意解决的。如果没有唯一的bean定义可用,则抛出异常。
In the latter scenario, you have several options: 在后一种情况下,您有几种选择: - Abandon autowiring in favor of explicit wiring.
- 放弃自动装配以支持显式装配。
- Avoid autowiring for a bean definition by setting its autowire-candidate attributes to false as described in the next section.
- 通过将autowire-candidate属性设置为false,避免自动装配bean定义,如下一节所述。
- Designate a single bean definition as the primary candidate by setting the primary attribute of its element to true.
- 通过将其元素的主属性设置为true,指定单个bean定义作为主要候选者。
- Implement the more fine-grained control available with annotation-based configuration, as described in Annotation-based container configuration.
- 如基于注释的容器配置中所述,实施基于注释的配置提供的更精细的控制。
Excluding a bean from autowiring 从自动装配中排除bean On a per-bean basis, you can exclude a bean from autowiring. In Spring’s XML format, set the autowire-candidate attribute of the element to false; the container makes that specific bean definition unavailable to the autowiring infrastructure (including annotation style configurations such as @Autowired). 在每个bean的基础上,您可以从自动装配中排除一个bean。在Spring的XML格式中,将元素的autowire-candidate属性设置为false;该容器使该特定的bean定义对自动装配基础结构不可用(包括诸如@Autowired之类的注释样式配置)。
The autowire-candidate attribute is designed to only affect type-based autowiring. It does not affect explicit references by name, which will get resolved even if the specified bean is not marked as an autowire candidate. As a consequence, autowiring by name will nevertheless inject a bean if the name matches. autowire-candidate属性旨在仅影响基于类型的自动装配。它不会影响按名称显式引用,即使指定的bean未标记为自动连线候选,也会得到解决。因此,如果名称匹配,通过名称自动装配将注入一个bean。
You can also limit autowire candidates based on pattern-matching against bean names. The top-level element accepts one or more patterns within its default-autowire-candidates attribute. For example, to limit autowire candidate status to any bean whose name ends with Repository, provide a value of Repository. To provide multiple patterns, define them in a comma-separated list. An explicit value of true or false for a bean definitions autowire-candidate attribute always takes precedence, and for such beans, the pattern matching rules do not apply. 您还可以根据与bean名称的模式匹配来限制自动导向候选项。顶级元素在其默认自动装配候选属性中接受一个或多个模式。例如,要将autowire候选者状态限制为名称以Repository结尾的任何Bean,请提供值 Repository。要提供多种模式,请在逗号分隔列表中定义它们。对于bean定义autowire-candidate属性,显式值true或false的值始终优先,对于这些bean,模式匹配规则不适用。
These techniques are useful for beans that you never want to be injected into other beans by autowiring. It does not mean that an excluded bean cannot itself be configured using autowiring. Rather, the bean itself is not a candidate for autowiring other beans. 这些技术对于不想通过自动装配注入其他bean的bean非常有用。这并不意味着排除的bean本身不能使用自动装配进行配置。相反,该bean本身不是自动装配其他bean的候选者。
|