好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

基于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

问题原因: 新工程中未进行数据源信息配置。如果去掉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://HdhCmsTestcnblogs测试数据/rinack/p/13225226.html

查看更多关于基于Spring Boot 排除自动配置的4个方法的详细内容...

  阅读:34次