Skip to content

Cyclic dependency with constructor injection #26703

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

Open
1zg12 opened this issue Mar 19, 2021 · 1 comment
Open

Cyclic dependency with constructor injection #26703

1zg12 opened this issue Mar 19, 2021 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task

Comments

@1zg12
Copy link
Contributor

1zg12 commented Mar 19, 2021

Background

Spring is discouraging use of filed injection, and in favor or constructor injection.

However, when the beans need to cross reference to each other. For example:

class BeanA{
    @Autowired
    public BeanA(BeanB beanB) {
        this.beanB= beanB;
    }
}

class BeanB{
    @Autowired
    public BeanB(BeanA beanA) {
        this.beanA= beanA;
    }

this will instead throw out exception

APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  beanA defined in URL [jar:file:/...class]
↑     ↓
|  beanB defined in URL [jar:file:/....class]
↑     ↓
|  xx (field xx) 

Changes

Instead of just saying "Field injection is not recommended", can we make it clear, by adding

Field injection is not recommended **however, please don't constructor injection with circular reference** ?

?

Related Issues

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Mar 19, 2021
@sbrannen
Copy link
Member

sbrannen commented Mar 19, 2021

@sbrannen sbrannen self-assigned this Mar 19, 2021
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Mar 19, 2021
@sbrannen sbrannen added this to the 5.3.6 milestone Mar 19, 2021
@sbrannen sbrannen modified the milestones: 5.3.6, 5.3.7 Apr 12, 2021
@sbrannen sbrannen modified the milestones: 5.3.7, 5.3.8 May 11, 2021
@sbrannen sbrannen modified the milestones: 5.3.8, 5.3.9 Jun 7, 2021
@sbrannen sbrannen modified the milestones: 5.3.9, 5.3.10 Jul 5, 2021
@sbrannen sbrannen modified the milestones: 5.3.10, 5.3.11 Sep 14, 2021
@sbrannen sbrannen modified the milestones: 5.3.11, 5.3.12 Oct 11, 2021
@sbrannen sbrannen modified the milestones: 5.3.12, 5.3.13 Oct 20, 2021
@jhoeller jhoeller modified the milestones: 5.3.13, 5.3.14 Nov 5, 2021
@jhoeller jhoeller modified the milestones: 5.3.14, 5.3.15 Dec 14, 2021
@sbrannen sbrannen modified the milestones: 5.3.15, 5.3.16 Jan 12, 2022
@sbrannen sbrannen modified the milestones: 5.3.16, 5.3.17 Feb 16, 2022
@sbrannen sbrannen changed the title Circular reference with constructor injection Cyclic dependency with constructor injection Mar 12, 2022
@sbrannen sbrannen modified the milestones: 5.3.17, 6.0.0-M4 Mar 16, 2022
@sbrannen sbrannen modified the milestones: 6.0.0-M4, 6.0.0-M5 May 10, 2022
@sbrannen sbrannen modified the milestones: 6.0.0-M5, 6.0.0-M6 Jun 7, 2022
@sbrannen sbrannen modified the milestones: 6.0.0-M6, 6.0.0-RC1 Aug 29, 2022
@sbrannen sbrannen modified the milestones: 6.0.0-RC1, 6.0.0-RC2 Oct 3, 2022
@sbrannen sbrannen modified the milestones: 6.0.0-RC2, 6.0.0 Oct 12, 2022
@sbrannen sbrannen modified the milestones: 6.0.0, 6.0.x Nov 15, 2022
@sbrannen sbrannen modified the milestones: 6.0.x, 6.1.x Jul 4, 2023
@sbrannen sbrannen modified the milestones: 6.1.x, 6.1.0-RC1 Aug 19, 2023
@sbrannen sbrannen modified the milestones: 6.1.0-M5, 6.1.x, 6.1.0-RC2 Sep 13, 2023
@sbrannen sbrannen modified the milestones: 6.1.0-RC2, 6.1.x Oct 10, 2023
@jhoeller jhoeller modified the milestones: 6.1.x, General Backlog Jan 11, 2024
@sbrannen sbrannen removed their assignment May 12, 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) type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants