1.4.5.1. Limitations and Disadvantages of Autowiring(自动装配的局限性和缺点)

 Spring Framework Documentation (5.3.10)

Core

IoC Container, Events, Resources, i18n, Validation, Data Binding, Type Conversion, SpEL, AOP.

   Core Technologies

1. The IoC Container

1.1. Introduction to the Spring IoC Container and Beans(Spring IoC容器和bean简介)

1.2. Container Overview (容器概览)

1.3. Bean Overview (Bean概览)

1.4. Dependencies(依赖)

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)

1.4.6. Method Injection(方法注入)

1.5. Bean Scopes(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 and constructor-arg settings always override autowiring. You cannot autowire 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 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, and Classes )(以及此类简单属性的数组)。这一限制是因设计导致的。
  • 自动装配不如显式装配精确。尽管如此,如前表所述,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 to false, as described in the next section.
  • Designate a single bean definition as the primary candidate by setting the primary attribute of its <bean/> element to true.
  • 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)中所述。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月满闲庭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值