Creating beans in a resolvable circular dependency graph that involves both construct injection and properties dose not always work #32711
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
status: declined
A suggestion or change that we don't feel we should currently apply
I have several beans with circular dependencies between them, some are using constructor injection and some property injection. The dependency graph should be construable by carefully creating beans in the right order and injecting beans before they have all of there properties set.
Spring IoC dose not seem to be able to resolve such a dependency graph, but will fail with an exception.
A simple minimal example with only two beans is:
Crating a
ApplicationContext
from this example results in an exception telling us we have a circular dependency. This dependency should however be able to be resolved as we are using property injection in one of the beans. By first creating "b", then creating "a" injecting "b" into the constructor and lastly injecting "a" into the setter in "b" we can create this set of beans.In this simple example we can resolve the problem by exchanging the order of the declarations of the beans in the xml file, so that "b" is declared before "a".
I should probably be able to solve the more complicated dependency graph in my application by carefully exchanging the order of declarations of beans and properties in the xml config. But this is error prune and unstable. Any small change to the resulting config may brake bean creation again.
The text was updated successfully, but these errors were encountered: