服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 基于Spring Boot 排除自动配置的4个方法

基于Spring Boot 排除自动配置的4个方法

2021-11-01 13:26如若 Java教程

这篇文章主要介绍了Spring Boot 排除自动配置的4个方法,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

Spring Boot 排除自动配置

方法1

使用 @SpringBootApplication 注解,用 exclude 属性进行排除指定的类:

?
1
2
3
4
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {
    // ...
}

方法2

单独使用 @EnableAutoConfiguration 注解的时候:

?
1
2
3
4
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
public class Application {
    // ...
}

方法3

使用 @SpringCloudApplication 注解的时候:

?
1
2
3
4
5
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@SpringCloudApplication
public class Application {
    // ...
}

方法4

终极方案,不管是 Spring Boot 还是 Spring Cloud 都可以搞定,在配置文件中指定参数 spring.autoconfigure.exclude 进行排除:

?
1
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

或者还可以这样写:

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

yml 配置文件:

?
1
2
3
4
spring:    
  autoconfigure:
    exclude:
      - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

Springboot项目去除自动配置

举例说明

新建了一个springboot工程,运行程序时报错:Reason: Failed to determine a suitable driver class

基于Spring Boot 排除自动配置的4个方法

问题原因: 新工程中未进行数据源信息配置。如果去掉springboot工程相关自动配置,该问题就不会出现了

解决办法:

?
1
2
3
4
5
6
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
public class UmeApiPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(UmeApiPlusApplication.class, args);
    }
}

总结

使用@SpringBootApplication(exclude = {})可去除springboot工程的自动配置。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/rinack/p/13225226.html

延伸 · 阅读

精彩推荐