Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

1.在編寫第一個hello springboot項目時遇到的bug。

Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

警告:你的應用上下文可能沒有啟動,因為你將註解添加到了默認的package上面了。下面的堆棧信息中也有一句話包括了這個意思。

……This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)。

情景:在編寫第一個hello springboot項目時遇到的bug。創建項目,導入依賴,編寫MainApplication.class,運行時報錯

2、我的項目結構是這樣的(我將MainApplication的文件建在了java文件夾下面)

image

3、原因解析

我並沒有使用@ComponentScanning註解,這裡為什麼會蹦出個這樣的註解呢?

SpringBoot在編寫啟動類(Main方法所在的類)的時候如果不使用@ComponentScan指明對象掃描範圍,默認指掃描當前啟動類所在的包里的對象。

(注意:我在編寫Main方法的時候並沒有加@ComponentScan註解,因而,他會掃描Application所在的包里的對象)
如果當前啟動類沒有包,則在啟動時會報錯:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package錯誤。

(注意:寫在java文件夾下的Application類,是不從屬於任何一個包的,因而啟動類沒有包)

4、解決辦法

方法一、將MainApplication建在其他的包下面

image

方法二、在MainApplication類上面加@ComponentScan註解,指定要掃描的包

@ComponentScan(basePackageClasses = HelloController.class)

image

參考文章:
//www.cnblogs.com/gudi/p/7892769.html