@SpringBootConfiguration重復(fù)加載報錯問題解決
Junit
單元測試@Test
啟動報錯,@SpringBootConfiguration
注解重復(fù)問題排查:
@SpringBootApplication
注解的 exclude
屬性用于排除特定的自動配置類,而不是用于排除主配置類本身。因此,不能通過 exclude
屬性來排除主配置類的加載。
正確的使用方式
如果你想排除某些自動配置類,可以使用 exclude
屬性。例如:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class PortalApplication { public static void main(String[] args) { SpringApplication.run(PortalApplication.class, args); } }
排除主配置類的加載
如果你確實(shí)需要在某些情況下排除主配置類的加載,可以考慮以下幾種方法:
1. 使用不同的配置類
你可以創(chuàng)建不同的配置類,并在不同的環(huán)境中使用不同的配置類。例如,可以創(chuàng)建一個測試配置類,并在測試中使用它。
// 主配置類 @SpringBootApplication public class PortalApplication { public static void main(String[] args) { SpringApplication.run(PortalApplication.class, args); } } // 測試配置類 @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
在測試類中使用 @SpringBootTest
注解來指定測試配置類:
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.junit.runner.RunWith; import org.junit.Test; @RunWith(SpringRunner.class) @SpringBootTest(classes = TestApplication.class) public class PortalApplicationTest { @Test public void contextLoads() { // 測試內(nèi)容 } }
2. 使用 @TestConfiguration
如果你只需要在測試中排除某些配置,可以使用 @TestConfiguration
注解來創(chuàng)建測試專用的配置類。
import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; @TestConfiguration public class TestConfig { @Bean public SomeBean someBean() { return new SomeBean(); } }
在測試類中使用 @Import
注解來導(dǎo)入測試配置類:
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.context.annotation.Import; import org.junit.runner.RunWith; import org.junit.Test; @RunWith(SpringRunner.class) @SpringBootTest(classes = PortalApplication.class) @Import(TestConfig.class) public class PortalApplicationTest { @Test public void contextLoads() { // 測試內(nèi)容 } }
總結(jié)
@SpringBootApplication
的exclude
屬性:用于排除特定的自動配置類,而不是用于排除主配置類本身。- 不同的配置類:可以創(chuàng)建不同的配置類,并在不同的環(huán)境中使用不同的配置類。
@TestConfiguration
:用于創(chuàng)建測試專用的配置類,并在測試中使用。
到此這篇關(guān)于@SpringBootConfiguration重復(fù)加載報錯的文章就介紹到這了,更多相關(guān)@SpringBootConfiguration重復(fù)加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java中system.exit(0) 和 system.exit(1)區(qū)別
本文主要介紹了Java中system.exit(0) 和 system.exit(1)區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05Java集合中的WeakHashMap、IdentityHashMap、EnumMap詳解
這篇文章主要介紹了Java集合中的WeakHashMap、IdentityHashMap、EnumMap詳解,HashMap的key保留了對實(shí)際對象的強(qiáng)引用,這意味著只要HashMap對象不被銷毀,還HashMap的所有key所引用的對象就不會被垃圾回收,需要的朋友可以參考下2023-09-09struts2中通過json傳值解決亂碼問題的實(shí)現(xiàn)方法
這篇文章主要介紹了struts2中通過json傳值解決亂碼問題的實(shí)現(xiàn)方法,涉及js編碼及java解碼的相關(guān)操作技巧,需要的朋友可以參考下2016-06-06關(guān)于在IDEA熱部署插件JRebel使用問題詳解
這篇文章主要介紹了關(guān)于在IDEA熱部署插件JRebel使用問題詳解,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12Java Spring使用hutool的HttpRequest發(fā)送請求的幾種方式
文章介紹了Hutool庫中用于發(fā)送HTTP請求的工具,包括添加依賴、發(fā)送GET和POST請求的方法,以及GET請求的不同參數(shù)傳遞方式,感興趣的朋友跟隨小編一起看看吧2024-11-11