Can I give some precedence in similar manner to my beans
Yes.
A) To define a specific order your Configuration classes will be handled (by the way, a Configuration class does not have to be annotated with @Configuration
(so-called full definition), but it's enough to be annotated with @Component
, @ComponentScan
, @Import
, @ImportResource
or just have a method annotated with @Bean
- so-called lite definition), you should
1) add your Configuration Candidates to your SpringApplication
's primarySource
, for example, in your main method like that
SpringApplication.run(
new Class[]{YourSpringBootApplication.class, Config1.class, Config2.class, ...},
args);
2) and annotate each of your Configuration Candidates with @Order
annotation, any other ordering means like Ordered
interface, @DependsOn
etc will be ignored by ConfigurationClassPostProcessor
, the order in the primarySource
array will also be ignored.
Then ConfigurationClassPostProcessor
will sort your Configuration Candidates and handle them according the @Order
annotation value you specified.
B) The precedence can also be achieved by defining your own AutoConfiguration classes. Although both Configuration and AutoConfiguration are handled by the same ConfigurationClassPostProcessor
, they are essentially distinctive machineries. To do so
1) define in your classpath /META-INF/spring.factories
file and put in the EnableAutoConfiguration section of it your AutoConfiguration classes like that
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
your.package.AutoConfig1,your.package.AutoConfig2
2) and annotate your AutoConfiguration classes with @AutoConfigureOrder
, @AutoConfigureAfter
, or @AutoConfigureAfter
annotations, any other ordering means again will be ignored.
Like @Strelok pointed out, AutoConfiguration classes, your own and provided e.g. by spring-boot-autoconfigure
library alike, will be added to the end of the list of Configuration Candidates.
Remember, however, that the order the Configuration Candidates will be handled by ConfigurationClassPostProcessor
does not necessarily coincide with the order the beans defined by the Configuration classes will be created. For example, you might define your Configuration class that overrides TomcatServletWebServerFactory
to make your own customization of Tomcat web server like
@Configuration
public class EmbeddedTomcatConfig {
@Bean
public TomcatServletWebServerFactory containerFactory() {
...
return customizedTomcatWebServerFactory;
}
but this method will be called right at the moment when your Spring Boot application decides to create a Web server, regardless of how you defined the precedence for your EmbeddedTomcatConfig
Configuration class.
Is Spring achieving this via some custom class loader
There is no need to. Although you could, as always with Spring, define your own ClassLoader
for BeanFactory
, standard ClassLoader
is good enough if everything you need for Configuration in your application is available in the classpath. Please notice, that at first phase ConfigurationClassPostProcessor
does not load (i.e. does not resolve) the Configuration candidates classes (otherwise, most of the classes in spring-boot-autoconfigure
library will fail to load). Instead it analyzes their annotations with bytecode analyzer, ASM
by default. For that purpose, it is just enough to get a binary form, a byte array, of a class to feed it to bytecode analyzer.