Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating beans in a resolvable circular dependency graph that involves both construct injection and properties dose not always work #32711

Closed
RickardNarsromLINK opened this issue Apr 25, 2024 · 1 comment
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

Comments

@RickardNarsromLINK
Copy link

RickardNarsromLINK commented Apr 25, 2024

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:

public class A {
public A(B b) { }
}
public class B {
public void setA(A a) { }
}
<beans>
<bean name="a" class="A">
<constructor-arg index="0" ref="b" />
</bean>
<bean name="b" class="B">
<property name="a" ref="a" />
</bean>
</beans>

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 25, 2024
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Apr 25, 2024
@snicoll
Copy link
Member

snicoll commented Apr 27, 2024

Unfortunately, circular dependency references are best effort and we might consider deprecate and remove the feature in a future release. The resolution follows the declaration order and therefore this is the expected behavior.

You can break the cycle by being more declarative about it, using @Lazy and ObjectProvider.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Apr 27, 2024
@snicoll snicoll added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

No branches or pull requests

4 participants