I am having some trouble getting some Spring configuration to be applied in the desired order with Spring Boot in a multi-module Maven project.
I have modules A and
Spring AutoConfiguration is used to provide a basic configuration if certain classes are in the classpath or not.
This is used e.g. to provide a basic Jpa configuration if Hibernate is on the classpath.
If you want to configure the order in which beans are instantiated by spring you can use
@DependsOn("A")
public class B{
...
}
This would create bean "A", than "B".
However, the order you desire may not be possible. You wrote :
A depends on C, B depends on A
If 'depends on' means : A needs C to be instantiated, the beans must be created in the following order :
Spring automatically detects the dependencies by analyzing the bean classes.
If A has an autowired property or a constructor argument of type C, spring 'knows' that it must instantiate C before A.
In most cases this works quite well.
In some cases spring cannot 'guess' the dependencies and creates beans in an unwanted order. Than you can 'inform' spring through the @DependsOn annotation about the dependency. Spring will try to change the order accordingly.
In your case, if the dependencies you described are not visible for spring, and the dependencies are not needed to create the beans, you can try to change the order with @DependsOn :
A depends on C, B depends on A
Could be achieved with
@DependsOn("C")
public class A{
...
}
@DependsOn("A")
public class B{
...
}
// C comes from another module
// and no need to annotate