欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-04-20 08:59:45|栏目:JAVA代码|点击:

Spring Boot 排除自动配置

方法1

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

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

方法2

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

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

方法3

使用 @SpringCloudApplication 注解的时候:

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

方法4

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

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

或者还可以这样写:

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

yml 配置文件:

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

Springboot项目去除自动配置

举例说明

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

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

解决办法:

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class })
public class UmeApiPlusApplication {
    public static void main(String[] args) {
        SpringApplication.run(UmeApiPlusApplication.class, args);
    }
}

总结

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

上一篇:在Spring异步调用中传递上下文的方法

栏    目:JAVA代码

下一篇:SpringBoot拦截器使用精讲

本文标题:基于Spring Boot 排除自动配置的4个方法

本文地址:http://www.codeinn.net/misctech/199693.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有