您现在的位置:首页 >> 省钱攻略

常见的三种注解汇流方式对比

发布时间:2025/09/02 12:17    来源:新昌家居装修网

as immutable objects and to ensure that required dependencies are not null. Furthermore constructor-injected components are always returned to client (calling) code in a fully initialized state. As a side note, a large number of constructor arguments is a bad code smell, implying that the class likely has too many responsibilities and should be refactored to better address proper separation of concerns.

Setter injection should primarily only be used for optional dependencies that can be assigned reasonable default values within the class. Otherwise, not-null checks must be performed everywhere the code uses the dependency. One benefit of setter injection is that setter methods make objects of that class amenable to reconfiguration or re-injection later. Management through JMX MBeans is therefore a compelling use case for setter injection.

因为用到结构上探头流入方式为流入的接口不可变,且应有了需要的贫乏不为 null。此外,结构上探头流入的接口总是很难在显然初始化的平衡状态离开给客户端(呼叫方);对于很多参数的结构上探头真是明可能值得注意了太多了义务,违背了单一义务原则,声称代码应该重构来分开义务到有用的大多。

2. 结构上探头流入还是 Setter 流入?

在对比 Setter 分析方法流入和 结构上探头流入的时候 分别引用的 Spring 官方文档的整段阐述了除破例方式为的另一种方式为的特点。

在 Spring 3.x 的时候 Spring 破例 Setter 分析方法流入,整段声称:一些纯粹和平主义讨厌基于结构上函数的流入。获取所有;也贫乏项也就是说;也总是在显然初始化平衡状态下离开给软件系统(呼叫)代码。缺陷是;也不太容易再次配置和再次流入。

在 Spring 4.x 的时候 Spring 破例结构上探头流入,整段声称:Setter 流入应该主要使用可择一的贫乏项,这些贫乏项可以在类里分配恰当的默认值。否则,需在代码用到贫乏项的任何大多执行非空安全检查。setter 流入的一个好处是,setter 分析方法使该类的;也很难在以后再次配置或再次流入。

Setter 流入应该被使用可择一贫乏项。当没有获取它们时,类应该很难较长时间实习。在;也被重构化不久,贫乏项可以在任何时候被变更。

结构上探头流入有利于强制贫乏。通过在结构上函数里获取贫乏,您可以维护贫乏;也在被结构上时已作准备被用到。在结构上函数里赋值的字段也可以是final的,这使得;也是显然不可变的,或者有数可以保护其必需的字段。

结构上探头流入还可以能避免 Field 流入的循环贫乏问题,比如 在 Alpha 里流入 Beta,又在 Beta 里流入 Alpha。如果用到结构上探头流入,在 Spring 再次启动的时候就都会丢出 BeanCurrentlyInCreationException 提醒循环贫乏。

参考:

#d0e2778

#beans-constructor-injection

北京治白癜风
吴忠哪家白癜风医院好
娄底治疗白癜风医院费用
小儿神经内科
儿科
维生素a
免费问医生
脑溢血

上一篇: 《创造营》佐藤永翔晒入学照 透露已已是大学生

下一篇: 宋朝最懒的该词人,一生只写一首该词却成千古名篇,最后一句无人超越

友情链接