spring-projects / spring-projects/spring-framework

Document how to optimize the dependency graph

Open
#30,333 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

in: core type: documentation
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

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

  • #25443
  • #26703
  • #27534

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the existing Spring documentation for @Configuration and the related issues #25443, #26703, and #27534. Document the tradeoffs of splitting configuration, static bean methods, and method arguments, including how method arguments can reduce circular dependencies; done means the guidance and example are added to the appropriate documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
62/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.