Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular dependency in Spring injection - Is this bad design?

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

like image 212
user3681970 Avatar asked Nov 19 '25 03:11

user3681970


1 Answers

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.

like image 132
Chetan Kinger Avatar answered Nov 20 '25 16:11

Chetan Kinger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!