Document how to optimize the dependency graph #30333
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
type: documentation
A documentation task
Milestone
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:
The reason this will fail is because
MyConfig
requiresMyFilter
, butMyFilter
requiresMyDependency
which is provided byMyConfig
. Spring cannot resolveMyDependency
because it is unable to create an instance ofMyConfig
withoutMyFilter
. 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:
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
The text was updated successfully, but these errors were encountered: