Spring Boot AutoConfiguration Order

I'm having trouble setting up any Spring configuration in the desired order using Spring Loading in a multi-module Maven project.

I have modules A and B, which are written by me and depend on a third-party module that I do not control in module C (the dependencies are as follows: A depends on C, B depends on A)

In module A, I have a class annotated with @Configuration , as well as @AutoConfigureBefore(ClassFromModuleD.class) . In module B, I have another class annotated with @Configuration and also @AutoConfigureBefore(ClassFromModuleA.class)

I was hoping this would lead to the definition of bean definitions in my module B and then beans in my module. The configuration class, and then finally the ones specified in C.

I also tried adding the META-INF/spring.factories to both modules A and B, which declares a single configuration file present in its own module. For instance. for module A

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.exmaple.moduleAConfiguration

and in module B:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.exmaple.moduleBConfiguration

I do not see the desired configuration order, in fact it seems to be the exact opposite of what I want. I used the logging instructions and the debugger for the transition, and it seems the configuration from module C is applied first, then A and then B.

Can someone point out what I might have missed, or if there is another way to do this? Thank you very much in advance.

+5
source share
1 answer

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{ ... } // C comes from another module // and no need to annotate 
0
source

Source: https://habr.com/ru/post/1209957/


All Articles