In SpringBoot, a very core function is the automatic configuration (Auto-Configuration) mechanism. In this article, let’s talk about 8 treasure techniques for implementing automatic configuration in SpringBoot. I hope to help you better understand the automatic configuration principles of SpringBoot.
1. Core annotation: @EnableAutoConfiguration
Automatic configuration is enabled mainly through@EnableAutoConfiguration
Annotation implementation, usually, this annotation is included in@SpringBootApplication
middle:
@SpringBootApplication public class MyApplication { public static void main(String[] args) { (, args); } }
@SpringBootApplication
It is equivalent to combining the following three annotations:
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
2. Automatically configure the registration of the class:
Spring BootFile (located in each automatic configuration module
META-INF
) to register all automatic configuration classes. Specifically,@EnableAutoConfiguration
Annotation will triggerAutoConfigurationImportSelector
, it will readIn the file
All autoconfiguration classes corresponding to the key and import them into the application context.
Examplecontent:
=\ ,\
3. Conditional assembly: @Conditional annotation
Automatic configuration classes usually use various@Conditional
Annotation to determine whether to apply a specific configuration. These conditions are based on classes in the class path, existing beans, configuration properties, etc. Common conditional annotations include:
-
@ConditionalOnClass
: Take effect when the specified class exists on the classpath. -
@ConditionalOnMissingBean
: Effective when the specified bean does not exist. -
@ConditionalOnProperty
: Take effect when a specific configuration property meets the condition. -
@ConditionalOnBean
: Effective when the specified bean exists.
Example:
@Configuration @ConditionalOnClass() @ConditionalOnMissingBean() public class DataSourceAutoConfiguration { @Bean public DataSource dataSource() { // Create and return the default data source } }
The above configuration indicates:DataSource
The class exists in the classpath and is not defined in the contextDataSource
Bean, automatically configure a default data source.
4. Priority and coverage of automatic configuration
Although automatic configuration will automatically configure many beans based on conditions, developers can override the default configuration in the following ways:
Custom Bean: If the developer defines a certain bean in the context and the automatic configuration class tries to define the same type of bean, usually the developer customization beans will take precedence over the automatically configured beans.
Exclude automatic configuration: Can be@SpringBootApplication
or@EnableAutoConfiguration
Used in commentsexclude
Properties to exclude specific autoconfiguration classes.
@SpringBootApplication(exclude = { }) public class MyApplication { ... }
Configuration Properties: Byor
Set specific configuration properties in the configuration properties that enable, disable, or customize the behavior of automatic configuration.
5. SpringBoot Starter
Spring Boot Starter is a set of dependency descriptors (usually Maven or Gradle dependencies) that aggregate a set of related dependencies. By introducing a specific Starter, the automatic configuration mechanism detects relevant dependencies and applies the corresponding automatic configuration as needed.
Common Starter examples:
-
spring-boot-starter-web
: Contains the dependencies required to build web applications, such asspring-webmvc
、Tomcat
etc. and trigger relevant automatic configuration (e.g.DispatcherServlet
、Tomcat
wait). -
spring-boot-starter-data-jpa
: Contains JPA-related dependencies and triggers automatic configuration of data sources, JPA entity managers, etc.
6. AutoConfigurationImportSelector and assembly automatic configuration
AutoConfigurationImportSelector
yes@EnableAutoConfiguration
The key category behind it. It is responsible for readingAutomatic configuration class in the file and applied to the Spring application context. The process is as follows:
-
Analysis: Read all in
Automatic configuration class registered in .
-
Evaluation conditions: For each automatic configuration class, evaluate its
@Conditional
Annotation, determine whether to apply the configuration. - Import configuration classes: Import the automatic configuration class that meets the criteria into the application context.
7. Automatic debugging and diagnostic configuration
Spring Boot provides some tools and features to help developers understand and debug automatic configuration:
spring-boot-starter-actuator
: Includedauto-configure
The endpoint can display the application's automatic configuration report.
@EnableAutoConfiguration
ofreport
Log: Shows what automatic configurations are applied or excluded in the startup log.
Properties: You can specify the automatic configuration class to exclude in the configuration file.
8. Customize automatic configuration
As developers, we can also create custom autoconfiguration classes to automatically configure specific beans under specific conditions.
The following are the 3 core steps:
Create an automatic configuration class:use@Configuration
and appropriate@Conditional
annotation.
@Configuration @ConditionalOnClass() public class MyServiceAutoConfiguration { @Bean public MyService myService() { return new MyService(); } }
existRegistered in:
=\
Publish Starter: Package the automatic configuration class in a Starter for other projects to be introduced and used.
Through the above 3 core steps, when we start the SpringBoot container, the customized classes will be automatically configured to achieve what we want to achieve.
9. Summary
In this article, we analyzed 8 treasure techniques for Spring Boot automatic configuration. Through a series of intelligent conditional judgment, dependency management and configuration file support, Spring can automatically assemble required components and beans according to the actual needs of the project. This greatly simplifies the configuration process of Spring applications, allowing developers to focus more on the implementation of business logic without caring about cumbersome configuration details.
This is the end of this article about 8 tips for SpringBoot automatic configuration. For more related content on SpringBoot automatic configuration, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!