7.4.5 Autowiring collaborators

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容器能够自动装配相关联的合作beans.你可以允许Spring通过检查 ApplicationContext的内容去自动解析合作者(其他的beans).自动装配有以下优点:

  • 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 autowire attribute of the <bean/> element. The autowiring functionality has four modes. You specify autowiring per bean and thus can choose which ones to autowire.

当使用基于XML配置元数据,你给一个bean定义指定自动装配模式通过 <bean/> 元素的 autowire 属性.自动装配功能有4中模式.你指定自动装配哪个bean,它就会选择哪个bean去自动装配.

Table 7.2. Autowiring modes

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.

(默认)不进行自动装配.bean引用必须通过 ref 元素被定义.

不建议在大型部署项目下改变这个默认的设置,

因为指定合作者可以明确地进行更好的控制和更清晰.

在一些程度上,它记录了一个系统的结构.

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 a setMaster(..) method),

Spring looks for a bean definition named master, and uses it to set the property.

通过name属性自动装配.Spring通过与这个属性具有相同名称来查找bean去自动装配.

例如,如果一个bean定义设置为通过名称去自动装配,

它包含了一个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进行自动装配.

如果这里没有匹配到beans,就什么都不会发生;这个属性也不会被设置.

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,如果预期的key类型是String.一个被自动装配的Maps值由匹配到的预期类型bean实例组成,这个Maps的keys将会包含相应的bean名称.

You can combine autowire behavior with dependency checking, which is performed after autowiring completes.

你可以将依赖检查和自动装配结合使用,依赖检查会在自动装配完成后执行.

猜你喜欢

转载自blog.csdn.net/weixin_41648566/article/details/80901825