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

Document how to optimize the dependency graph #30333

Open
rwinch opened this issue Apr 13, 2023 · 2 comments
Open

Document how to optimize the dependency graph #30333

rwinch opened this issue Apr 13, 2023 · 2 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: documentation A documentation task

Comments

@rwinch
Copy link
Member

rwinch commented Apr 13, 2023

At times users can get into scenarios where they are getting circular dependencies and it is due to how they have structured their configuration.

For example, the following will fail due to circular bean dependencies:

@Component
class MyFilter {
    MyFilter(MyDependency dependency) {}
}

@Configuration
class MyConfig {
    MyFilter myFilter;

    MyConfig(MyFilter myFilter) {
        this.myFilter = myFilter;
    }

    @Bean
    MyOther myOther() {
       return new MyOther(this.myFilter);
    }

    @Bean
    MyDependency myDependency() {
        return new MyDependency();
    }
}

The reason this will fail is because MyConfig requires MyFilter, but MyFilter requires MyDependency which is provided by MyConfig. Spring cannot resolve MyDependency because it is unable to create an instance of MyConfig without MyFilter. Spring has the same problem if autowiring member variables because it does not know if the member variable is required for instantiating Beans defined in non-static methods.

There are a few ways of minimizing circular dependencies that I'm aware of:

  • Splitting up the Configuration into more classes (can be difficult with large object graphs)
  • Using static methods for Bean declarations
  • Using arguments to the method of the bean definition instead of member variables in Configuration classes

To me the best option is to use method arguments for resolving dependencies is the best option because it provides the most precise definition of the dependency graph.

I think it would be good if Spring documented some of the tradeoffs and benefits of how to use @Configuration. At minimum, I believe it would be beneficial to add something stating that using arguments to the methods to define Bean methods can solve many circular dependencies problems.

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 Apr 13, 2023
@sbrannen sbrannen added type: documentation A documentation task in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 17, 2023
@sbrannen sbrannen self-assigned this Apr 17, 2023
@sbrannen sbrannen added this to the 6.0.x milestone Apr 17, 2023
@sbrannen
Copy link
Member

@Ankitbagga75
Copy link

Hi @sbrannen
should New Documentation file be created or There is any existing file where we can add Documentation?

@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
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

5 participants