The auto configuration provided by Spring Boot is very powerful. In some cases, the auto configuration function may not meet our requirements. We need to customize the configuration. At this time, we need to exclude / disable the auto configuration of some classes of Spring Boot.
For example, data source and mail are all provided with automatic configuration. We need to exclude the automatic configuration of Spring Boot and give it to ourselves to customize. How can we do this?
Today, the head of the stack will introduce you four ways of exclusion. There will always be one that can help you!
Method 1
When using the @ SpringBootApplication annotation, use the exclude property to exclude the specified class:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class}) public class Application { // ... }
When the auto configuration class is not under the class path, use the excludeName property to exclude the specified class name full path:
@SpringBootApplication(excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration", "org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration"}) public class Application { // ... }
This annotation integrates the @ EnableAutoConfiguration annotation and its parameters, which need not be explained. See this article for details: Three core notes of Spring Boot . In addition, pay attention to the Java technology stack WeChat public number, reply in the background: boot, you can get more Spring Boot dry goods.
Method 2
When using @ EnableAutoConfiguration annotation alone:
@... @EnableAutoConfiguration (exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class}) public class Application { // ... }
When the auto configuration class is not under the class path, use the excludeName property to exclude the specified class name full path:
@... @EnableAutoConfiguration {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration", "org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration"}) public class Application { // ... }
Method 3
When using Spring Cloud and @ SpringCloudApplication annotations:
@... @EnableAutoConfiguration (exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class}) @SpringCloudApplication public class Application { // ... }
Spring Cloud must be built on the Spring Boot application, so there is no need to explain this.
Method 4
The ultimate solution, whether Spring Boot or Spring Cloud, can be solved by specifying the parameter spring.autoconfigure.exclude in the configuration file to exclude:
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\ org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration
Or write as follows:
spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration
If you are using a yaml configuration file, you can write as follows:
spring: autoconfigure: exclude: - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration - org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration
Knowing these four exclusion methods, we can easily use Spring Boot's auto configuration function. How about that? Did you get it? Recommendation + + collection, then set the top of the public number.
Well, today's sharing will be here. More Spring Boot articles are being written, focusing on the Java technology stack WeChat public number to get the first push. In the background of public address reply: boot, you can also get the Spring Boot tutorials with long stack arrangement. They are all actual dry goods. The following is only partial preview.
- Several ways to read configuration from Spring Boot
- How does Spring Boot do parameter verification?
- The core 25 comments of Spring Boot!
- Source code analysis of the whole process of Spring Boot 2.x startup
- Spring Boot 2.x new features summary and migration guide
- ......