Spring注入中的循环依赖-这是不好的设计吗? [英] Circular dependency in Spring injection - Is this bad design?
问题描述
我陷于以下问题:
我正在尝试如下创建bean:
@Bean公共abc createABC(){返回新的ABC(-,def(),-);}
`
@Bean公共DEF def(){返回新的DEF(-,createABC(),-}
任何解决该问题的建议,而无需追求基于二传手的注入.它表明设计不良吗?在我的情况下,这种依赖性是必须的.请提供您对此的看法
表明设计不良吗?
绝对.如果 ABC
依赖于 DEF
,而 DEF
依赖于 ABC
,则间接意味着您的代码未正确组织.这种周期性的依赖关系通常表明您没有遵守单一责任原则.
ABC
具有 DEF
应该具有的逻辑,反之亦然.您应该重构这些类,以使 ABC
取决于 DEF
或 DEF
取决于 ABC
,但不能同时取决于这两个./p>
I am stuck with following issue :
I am trying to create beans as follows:
@Bean
public abc createABC() {
return new ABC(--, def(),--);
}
`
@Bean
public DEF def() {
return new DEF(--, createABC(),--
}
Any suggestions to get around this problem without chaging to setter based injection. Is it the indicative of bad design? In my situation this dependency is must. Please provide your viewpoints on this
it the indicative of bad design?
Absolutely. If ABC
depends on DEF
and DEF
depends on ABC
, it indirectly means that your code has not been organized correctly. Such cyclic dependencies usually indicate that you are not adhering to the Single Responsibility Principle.
ABC
has logic that DEF
should have and vice-versa. You should refactor these classes such that either ABC
depends on DEF
or DEF
depends on ABC
but not both.
这篇关于Spring注入中的循环依赖-这是不好的设计吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!