Four methods of Spring Boot excluding automatic configuration

The automatic configuration provided by Spring Boot is very powerful. In some cases, the automatic configuration function may not meet our needs. We need to customize the configuration. At this time, we need to exclude / disable the automatic configuration of some classes of Spring Boot.

For example, data sources and e-mail provide automatic configuration. We need to exclude the automatic configuration of Spring Boot and leave it to ourselves to define. What should we do?

Today, the stack leader will introduce you to four exclusion methods, one of which can help you!

Method 1

use  @ SpringBootApplication   When commenting, use the exclude attribute to exclude the specified class:

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class})
public class Application {
    // ...
}

When the auto configuration class is not in the class path, use the excludeName property to exclude the full path of the specified class name:

@SpringBootApplication(excludeName = {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration", "org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration"})
public class Application {
    // ...
}

This annotation integrates  @ EnableAutoConfiguration   There is no need to explain the annotation and its parameters. See this article for details: Detailed explanation of the three core annotations of Spring Boot . In addition, pay attention to the Java technology stack WeChat official account, reply in the background: boot, you can get more dry cargo Spring Boot.

Method 2

Use alone  @ EnableAutoConfiguration   When commenting:

@...
@EnableAutoConfiguration
(exclude = {DataSourceAutoConfiguration.class, MailSenderAutoConfiguration.class})
public class Application {
    // ...
}

When the auto configuration class is not in the class path, use the excludeName property to exclude the full path of the specified class name:

@...
@EnableAutoConfiguration {"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration", "org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration"})
public class Application {
    // ...
}

Method 3

Using Spring Cloud and  @ SpringCloudApplication   When commenting:

@...
@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 parameters in the configuration file   spring.autoconfigure.exclude   Exclude:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
                             org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration

Or you can write:

spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration

If you use yaml configuration file, you can write:

spring:     
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
      - org.springframework.boot.autoconfigure.mail.MailSenderAutoConfiguration

Knowing these four exclusion methods, we can easily use the automatic configuration function of Spring Boot. How about it? Have you got it yet? It is recommended to forward + collect, and don't get lost in the future~

Well, today's sharing is here. More Spring Boot articles are being written, and the Java technology stack WeChat official account is being pushed for the first time.

Keywords: Java

Added by priti on Thu, 04 Nov 2021 10:03:43 +0200