當(dāng)前位置:
首頁 >
SpringBoot—— @ComponentScan
發(fā)布時間:2025/7/14
37
豆豆
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot—— @ComponentScan
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
2019獨角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
SpringBoot在寫啟動類的時候如果不使用@ComponentScan指明對象掃描范圍,默認(rèn)指掃描當(dāng)前啟動類所在的包里的對象,如果當(dāng)前啟動類沒有包,則在啟動時會報錯:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package錯誤。
因為啟動類不能直接放在main/java文件夾下(否則會報錯),必須要建一個包把它放進(jìn)去或者使用@ComponentScan指明要掃描的包。代碼示例如下:
@SpringBootApplication @ComponentScan(basePackageClasses=MytestApplication.class)//掃描該類所在的包 //或者使用basePackage屬性直接指定包 public class MytestApplication {public static void main(String[] args){SpringApplication.run(MytestApplication.class, args);} }轉(zhuǎn)載于:https://my.oschina.net/u/3786691/blog/1845023
總結(jié)
以上是生活随笔為你收集整理的SpringBoot—— @ComponentScan的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 聊聊resilience4j的Circu
- 下一篇: SpringCloud eureka服务