Spring Framework Documentation (5.3.10)
Core | IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP. |
1. The IoC Container
1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)
1.2. Container Overview (容器概览)
1.4.1. Dependency Injection(依赖注入)
1.4.1.1. Constructor-based Dependency Injection(基于构造函数的依赖注入) (Part I)
1.4.1.1. Constructor-based Dependency Injection(基于构造函数的依赖注入) (Part II)
1.4.1.2.Setter-based Dependency Injection(基于Setter的依赖注入)
1.4.1.3.Dependency Resolution Process(依赖解析过程)
1.4.1.4. Examples of Dependency Injection(依赖注入示例)
1.4.2. Dependencies and Configuration in Detail(依赖与配置详细介绍)
1.4.3. Using depends-on(使用depends-on)
1.4.4. Lazy-initialized Beans(延迟初始化Bean)
1.4.5. Autowiring Collaborators(自动装配协作者)
1.4.6.1. Lookup Method Injection(查找方法注入)
1.4.6.2. Arbitrary Method Replacement(任意方法替换)
下载此文档精编完整版
No. | 内容 | 下载地址 | 文档内容目录 |
1 | 中英双语精编版 第一部分 | PDF下载 | 内容目录 |
2 | 中英双语精编版 第二部分 | PDF下载 | 内容目录 |
3 | 中文精编版 第一部分 | PDF下载 | 内容目录 |
4 | 中文精编版 第二部分 | PDF下载 | 内容目录 |
更多章节内容,请点击查看: Core Technologies
1.4.5. Autowiring Collaborators(自动装配协作者)
The Spring container can autowire relationships between collaborating beans. You can let Spring resolve collaborators (other beans) automatically for your bean by inspecting the contents of the ApplicationContext
. Autowiring has the following advantages:
Spring容器可以自动装配(autowire)协作bean之间的关系。通过检查ApplicationContext
的内容,您可以让Spring为您的bean自动解析协作者(其他bean)。自动装配(Autowiring)具有以下优点:
- 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.)
- 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.
- 自动装配(Autowiring)可以显著减少指定属性或构造函数参数的需要(在这方面,本章其他地方讨论的其他机制(如bean模板)也很有价值。)
- 自动装配可以随着对象的演化(evolve)而更新配置。例如,如果您需要向类添加依赖项,则可以自动满足该依赖项,而无需修改配置。因此,在开发过程中,自动装配(autowiring)特别有用,而不需要在代码库变得更稳定时取消切换到显式连接(explicit wiring)的选项。
When using XML-based configuration metadata (see Dependency Injection), you can specify the 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 can thus choose which ones to autowire. The following table describes the four autowiring modes:
使用基于XML的配置元数据时(请参见依赖注入(Dependency Injection)),可以使用<bean/>元素的autowire
属性为bean定义指定自动装配模式。自动装配功能有四种模式。您可以为每个bean指定自动装配,从而可以选择要自动装配的bean。下表介绍了四种自动装配模式:
Table 2. Autowiring modes | |
Mode | Explanation |
| (Default) No autowiring. Bean references must be defined by |
| 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 |
| Lets a property 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 |
| Analogous to |
Table 2. 自动装配模式 | |
Mode | Explanation |
| (默认)无自动装配。Bean引用必须由ref元素定义。对于较大的部署,不建议更改默认设置,因为明确指定协作者可以提供更大的控制和清晰度。在某种程度上,它记录了一个系统的结构。 |
| 按属性名自动装配。Spring查找与需要自动装配的属性同名的bean。例如,如果一个bean定义将自动装配设置为按名称装配,并且它包含一个master属性(即,它有一个setMaster |
| 如果容器中正好存在一个属性类型(property type)的bean,则允许自动装配属性。如果存在多个异常,将抛出一个致命异常(fatal exception),这表示您可能不会对该bean使用byType自动装配。如果没有匹配的bean,则不会发生任何事情(未设置属性)。 |
| 类似于byType,但适用于构造函数参数。如果容器中没有一个构造函数参数类型的bean,则会抛出致命错误(fatal error)。 |
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 Map
instances if the expected key type is String
. An autowired Map
instance’s values consist of all bean instances that match the expected type, and the Map
instance’s keys contain the corresponding bean names.
使用byType
或constructor
自动装配模式,可以装配数组和类型化集合。在这种情况下,将提供容器中与预期类型匹配的所有自动装配候选项(autowire candidate)以满足依赖关系。如果所需的键类型(key type)为String
,则可以自动装配Map
实例。自动装配Map
实例的值由与预期类型匹配的所有bean实例组成,Map
实例的键(key)包含相应的bean名称。