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://www.cnblogs.com/rinack/p/13225226.html