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