Allows library developers to automatically configure beans in the Spring context based on different conditions of the application:
presence of certain classes in the classpath
existence of a bean
activation of some property.
The AutoConfiguration mechanism is guaranteed to occur after all the user-defined beans have been registered.
We can define our own bean and override the default behavior
Config
You can disable custom auto-configuration classes in @SpringBootApplication
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })// or if using without @SpringBootApplication@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })public class MyApplication {}
Create Custom AutoConfiguration
Classes that implement auto-configuration are annotated with @AutoConfiguration
This annotation itself is meta-annotated with @Configuration
You need to define auto-configuration classes in META-INF directory
if JpaTransactionManager type bean is not already present
@ConditionalOnClass(SomeService.class)
d
@ConditionalOnMissingClass()
d
@ConditionalOnProperty
@ConditionalOnResource
auto-configuration classes should not enable component scanning to find additional components.
Specific @Import annotations should be used instead.
If your configuration needs to be applied in a specific order, you can use the before, beforeName, after and afterName attributes in @AutoConfiguration