greetings all sometime in my app the logic forces me to use circular reference and i want to know how to enable this configuration in spring ?
Take a look at this article.
I would advice from the first option there. Circular dependencies are not a good sign and should be avoided. You can create a 3rd, helper class, for example.
The other two options are workarounds and will be harder to understand and debug later on.
Spring has no problems with circular references. BeanA
can be wired with BeanB
, and vice versa. You only get problems with circular references if you introduce them yourself in your code.
If you have a specific issue, please elaborate.
My solution for circular references splits objects into two, which share an interface. One will be the implementation, and the other will be a delegate. The implementation is initialized with a reference to the delegate and it injects the reverse dependency into the delegate in its init method.
By doing this, Spring will only see references pointing from the implementation towards the delegate. This won't work if any objects need to use the proxy in their initialization but that's probably a much easier problem to solve.
More info in my blog post here http://james.mega-global.com/2012/08/avoid-circular-references-in-spring-ioc/.