Circular dependency in Spring injection - Is this bad design?

淺唱寂寞╮ 提交于 2021-01-27 13:25:56

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/41868539/circular-dependency-in-spring-injection-is-this-bad-design

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!