The Spring Boot Auto-Configuration feature simplifies: automatic configuration of beans and components. When Spring Boot detects specific libraries or components in your project (like database dependencies or web modules), it automatically configures those components for you without needing explicit manual configuration.
Key Concepts:
- Auto-Configuration Conditions: Spring Boot applies auto-configuration on conditional and profile driven way to be classes available on the classpath or specific properties set in
application.properties
orapplication.yml
. - EnableAutoConfiguration: This annotation enables auto-configuration, it can be fine-tuned by excluding specific auto-configurations using
exclude
orexcludeName
attributes. - @Conditional Annotations: Auto-configuration relies heavily on Spring’s
@Conditional
annotations, which control whether certain configurations should be applied based on the environment or classpath. - Diagnostics:
spring-boot-actuator
provides insight into which configurations have been applied, helping with debugging and understanding the auto-configured components.
Documentation here.
Auto-Configuration Basics
Auto-configuration in Spring Boot helps developers automatically wiring beans for libraries included in the project. The process is based on a set of conditions, like a particular class is present in the classpath or certain properties are defined.
In Spring Boot, auto-configuration can be enabled through the @EnableAutoConfiguration
annotation. It instructs Spring Boot to apply default configurations based on the dependencies present in the application.
Auto-Configuration Entry Points (Version 1.x and 2.x)
In Spring Boot 1.x and 2.x, the entry point for auto-configuration classes is the spring.factories
file. This file contains a list of configuration classes under the key org.springframework.boot.autoconfigure.EnableAutoConfiguration
. When the application starts, Spring Boot scans this file, locates the configuration classes, and loads them as part of the auto-configuration process.
The spring.factories
file typically looks like this:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.config.MyAutoConfiguration,\
com.example.config.AnotherAutoConfiguration
This mechanism provides the mapping between Spring Boot’s @EnableAutoConfiguration
annotation and the configuration classes that should be auto-configured.
Property Injection with Spring Boot Auto-Configuration
This topic required more in-depth exploration, so I covered it in another post:
Evolution in Spring Boot 3.x: org.springframework.boot.autoconfigure.AutoConfiguration.imports
Starting with Spring Boot 3.x, the method for loading auto-configuration classes has changed. Instead of using spring.factories
, auto-configuration classes are now listed in a resource file located at META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
.
An example of the new AutoConfiguration.imports
file would look like this:
com.example.config.MyAutoConfiguration
com.example.config.AnotherAutoConfiguration
This change was introduced to address performance issues and limitations associated with the older spring.factories
mechanism. For further details see Spring Boot issue #29698, the shift was motivated by the need for better control over the order in which configuration classes are loaded, plus reducing the overhead of scanning all classes listed in spring.factories
during application startup.
Defining and Customizing Auto-Configuration
To define a custom auto-configuration class in Spring Boot, you can create a configuration class annotated with @Configuration
. This class is then referenced either through spring.factories
(in versions 1.x/2.x) or through AutoConfiguration.imports
(in version 3.x).
@Configuration
@ComponentScan("com.example.beans")
public class MyAutoConfig { }
To further enhance auto-configuration, and provide config variables is possible through annotations. For example, to create an annotation that enables your custom auto-configuration:
@AutoConfigurationPackage
@Import(ConfigSelector.class)
public @interface EnableMyautoConf {}
The ConfigSelector
class, implementing the ImportSelector
interface, can be used to programmatically select which configuration classes to load based on specific conditions:
public class ConfigSelector implements ImportSelector {
AnnotationAttributes attr = AnnotationAttributes.fromMap(
importClassMetadata.getannotationAttributes(
EnableMyautoConf.class.getName(),
false)
) // use any attribute from your main app defined annotation
public String[] selectImports(AnnotationMetadata importClassMetadata) {
return new String[] {
attr.get("key")==null
? "com.path.for.your.SelectedConfig"
: attr.get("key").toString() // do not dare this in real life :D
}
}
This approach enables fine-grained control over which configurations are loaded, depending on the context or environment in which the application is running.
Customizing Auto-Configuration with Conditions
In Spring Boot you can further utilize conditional annotations (@Conditional
, @ConditionalOnClass
, @ConditionalOnMissingBean
, etc.) to control when a particular configuration should be applied. Conditions handle the necessary configurations are loaded, based on classpath dependencies, property values, or other runtime conditions.
To conditionally load a bean based a specific class is on the classpath, you could use:
@Bean
@ConditionalOnClass(name = "com.path.config.SomeClass")
public MyService myService() {
return new MyServiceImpl();
}
This is useful with optional dependencies or when you want to provide default configurations that can be overridden by custom beans.
Conclusion
Hope I successfully demistified the complexity behind Spring Boot’s auto-configuration mechanism. Discovered multiple options to load or refine complex ways the you confgurations for the client app in question.
With these tools, one can easily create custom auto-configurations and manage them efficiently, while still taking advantage of Spring Boot’s powerful default configurations.
One thought on “Discover the Different Auto-Configuration Options in Spring Boot”