Spring AutoConfiguration is used to provide basic configuration if certain classes are in the class path or not.
It is used, for example. to provide a basic Jpa configuration if Hibernate is in the classpath.
If you want to customize the order in which the beans are created by spring, you can use
@DependsOn("A") public class B{ ... }
This would create a bean "A" than a "B".
However, the desired order may not be possible. You wrote:
A depends on C, B depends on A
If "depends on" means: Required C instances, beans must be created in the following order:
- C - because C is independent of nothing
- A - because A depends on C, which is already created.
- B - because B depends on A, which is already created.
Spring automatically detects dependencies by parsing bean classes.
If A has an autwired property or a cosntructor argument of type C, spring 'knows' that it must instantiate C before A.
In most cases, this works well.
In some cases, spring cannot βguessβ the dependencies and creates beans in an undesirable order. How can you "tell" spring through the @DependsOn annotation about dependency. spring will try to reorder accordingly.
In your case, if the described dependencies are not visible for spring, and the dependencies are not needed to create beans, you can try changing the order with @DependsOn:
A depends on C, B depends on A
Can be achieved with
@DependsOn("C") public class A{ ... } @DependsOn("A") public class B{ ... }