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.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.5.1. Limitations and Disadvantages of Autowiring(自动装配的局限性和缺点)
1.4.5.2. Excluding a Bean from Autowiring(在自动装配中排除bean)
下载此文档精编完整版
No. | 内容 | 下载地址 | 文档内容目录 |
1 | 中英双语精编版 第一部分 | PDF下载 | 内容目录 |
2 | 中英双语精编版 第二部分 | PDF下载 | 内容目录 |
3 | 中文精编版 第一部分 | PDF下载 | 内容目录 |
4 | 中文精编版 第二部分 | PDF下载 | 内容目录 |
更多章节内容,请点击查看: Core Technologies
1.4.5.1. 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.
Consider the limitations and disadvantages of autowiring:
自动装配在整个项目中一致使用时效果最佳。如果不总体使用自动装配,那么使用它仅装配一个或两个bean定义可能会让开发人员感到困惑。
考虑自动装配的局限性和缺点:
- Explicit dependencies in
property
andconstructor-arg
settings always override autowiring. You cannot autowire simple properties such as primitives,Strings
, andClasses
(and arrays of such simple properties). This limitation is by-design. - Autowiring is less exact than explicit wiring. Although, as noted in the earlier 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.
- Wiring information may not be available to tools that may generate documentation from a Spring container.
- 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
Map
instances, 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.
property
和constructor-arg
设置中的显式依赖项始终覆盖自动装配。不能自动装配简单属性,例如原始类型、字符串和类(primitives,Strings
, andClasses
)(以及此类简单属性的数组)。这一限制是因设计导致的。- 自动装配不如显式装配精确。尽管如此,如前表所述,Spring小心避免在可能产生意外结果的歧义情况下进行猜测。Spring托管对象(Spring-managed object)之间的关系不再明确记录。
- 装配信息可能无法用于从Spring容器生成文档的工具。
- 容器中的多个bean定义可能与要自动装配的setter方法或构造函数参数指定的类型相匹配。对于数组、集合或
Map
实例,这不一定是问题。然而,对于期望单个值的依赖项,这种模糊性并不是最终能解决的。如果没有唯一的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 tofalse
, as described in the next section. - Designate a single bean definition as the primary candidate by setting the
primary
attribute of its<bean/>
element totrue
. - Implement the more fine-grained control available with annotation-based configuration, as described in Annotation-based Container Configuration.
在后一种情况下,您有几个选项:
- 放弃自动装配,支持显式装配。
- 如下一节所述,通过将bean定义的
autowire-candidate
属性设置为false来避免自动装配。 - 通过将其<bean/>元素的
primary
属性设置为true,将单个bean定义指定为主要候选项(primary candidate)。 - 通过基于注解的配置实现更细粒度控制,如基于注解的容器配置(Annotation-based Container Configuration)中所述。