3

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

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
user3681970
  • 1,201
  • 4
  • 19
  • 37
  • The way out is setter based injection. At the end of the day you have to create one bean after the other. Whatever you create first cannot have the other passed in the constructor call. – Henry Jan 26 '17 at 07:27
  • @Jim Garrison The OP also wants to know if this is bad design. Can you point me to an answer in the duplicate that explains whether this is indicative of bad design? While this question does talk about spring and cyclic dependencies, it is not a duplicate since the OP clearly states he doesn't want to resort to setter based injection. Can you reconsider the veto on closing the question? – Chetan Kinger Jan 26 '17 at 07:42
  • You have to use setter-based injection since you must construct the objects first and then inject the mutual dependency references. This cannot be done at construction time. – Jim Garrison Jan 26 '17 at 07:47
  • @JimGarrison There are other options. One can always refactor the classes to remove the cyclic dependency. The OP is open to redesigning the classes because he wants to know if this is bad design. I don't see how this is a duplicate? – Chetan Kinger Jan 26 '17 at 07:48
  • Thanks all for the inputs! I am refactoring few chunks of code which will help in not creating such dependency. I believe this as an indicator of bad design (open for discussion). Since every classes should have well designed responsibilities so in ideal world this kind of situation should not arise. – user3681970 Jan 26 '17 at 07:50
  • You're free to vote to reopen. Ask on [meta] and if enough people agree it will get reopened. – Jim Garrison Jan 26 '17 at 07:50
  • @JimGarrison Well I can always do that but I would definitely ask you to give it a thought and reopen. I don't believe a full fledged street march/revolt is required since it is apparent from the above comment from the OP that this is a different question. I don't know why you think otherwise? – Chetan Kinger Jan 26 '17 at 07:53
  • Some references on circular dependencies: http://stackoverflow.com/questions/37444940/spot-problems-with-circular-dependency/37445480#37445480 – jaco0646 Jan 26 '17 at 13:55

1 Answers1

7

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.

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
  • I am not sure it is always an indicative of bad design. In my case I have 2 services **A** and **B**. **A** depends on **B** and **B** depends on **A**. They are injected into one another. Both have an **update** method. The reason I need this kind of setup is when I call **A.update()** I want **B.update()** to be called also, and the other way around. How is this a bad design? – IonutB Aug 10 '19 at 18:56
  • 2
    Why not have a class to compose those calls rather than coupling up the update methods to each other? Why even have a seperate A and B update if they need to both happen why not have an update service that accepts a and b and has an update method? – Darren Forsythe Aug 10 '19 at 20:37