Spring Boot is a way to exclude automatic configuration, which is very worth seeing!!!

Posted by shwanky on Mon, 03 Jan 2022 15:43:55 +0100

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

When using @ SpringBootApplication @ annotation, 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 the @ EnableAutoConfiguration annotation and its parameters. There is no need to explain this. See this article for details: the three core annotations of Spring Boot. In addition, you can get more Spring Boot dry goods by paying attention to the Java technology stack.

Method 2

When using @ EnableAutoConfiguration annotation alone:

@...
@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

When using Spring Cloud and @ SpringCloudApplication annotation:

@...
@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. Specify the parameter spring. In the configuration file autoconfigure. 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, that's all for today's sharing. More Spring Boot articles are being written. If you want more content, you can pay attention to the Xiaobian.

Topics: Java Spring Programmer