How to use @AutoConfigureOrder in a spring boot configuration class from a module

Viewed 1466

In my spring boot project I use a module, which provides a @Configuration class. The configuration ist imported via @Import(ConfigurationFromModule.class) in Application class.

The configuration from the module needs to happen before a certain AutoConfiguration. Unfortunately annotations defining the order of configurations seem to be ignored in the modules classes.

I annotated the Configuration with @AutoConfigureBefore({SomeBuildInAutoConfiguration.class}), but still SomeBuildInAutoConfiguration was triggered before ConfigurationFromModule.

Out of curiosity I tried to annoatate ConfigurationFromModule with @AutoConfigureOrder(1) and @Primary - but they don't change anything.

For @Configurationclasses within the main applications all those annotations work as expected.

How to define order of AutoConfiguration within a module?

1 Answers

Spring AutoConfiguration provides a basic configuration if certain classes are in the classpath or not.

If you want that configuration in order you can make use of @DependsOn

@DependsOn("One") 
public class Two{
    
}

This helps you to create bean "One", then bean "Two".

Spring automatically detects the dependencies by analyzing the bean classes.

If Bean One has an autowired property or a constructor argument of type Two, spring knows that it must instantiate Two before One.

Related