【SpringBoot】ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

報錯信息

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.4.RELEASE)

2019-06-12 20:24:20.516  INFO 14872 --- [           main] App                                      : Starting App on DESKTOP-11VR9V9 with PID 14872 (F:\501.JavaProject\PMS\target\classes started by MengChengdu in F:\501.JavaProject\PMS)
2019-06-12 20:24:20.519  INFO 14872 --- [           main] App                                      : No active profile set, falling back to default profiles: default
2019-06-12 20:24:20.548  INFO 14872 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5db45159: startup date [Wed Jun 12 20:24:20 CST 2019]; root of context hierarchy
2019-06-12 20:24:20.556  WARN 14872 --- [           main] ionWarningsApplicationContextInitializer : 

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


2019-06-12 20:24:30.683  WARN 14872 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL
2019-06-12 20:24:30.687  INFO 14872 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-12 20:24:30.693 ERROR 14872 --- [           main] o.s.boot.SpringApplication               : Application run
failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL 
[jar:file:/C:/Users/TR/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.0.4.RELEASE/spring-boot-
autoconfigure-2.0.4.RELEASE.jar!/

Caused by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.
autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration due to org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType not found. 
Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning 
a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)

	at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:55) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	... 20 common frames omitted
Caused by: java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType
	at org.springframework.boot.jdbc.EmbeddedDatabaseConnection.<clinit>(EmbeddedDatabaseConnection.java:50) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseCondition.getMatchOutcome(DataSourceAutoConfiguration.java:148) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	at org.springframework.boot.autoconfigure.condition.SpringBootCondition.matches(SpringBootCondition.java:47) ~[spring-boot-autoconfigure-2.0.4.RELEASE.jar:2.0.4.RELEASE]
	... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType
	at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_181]
	... 29 common frames omitted

問題分析

報錯信息看上去很長(Java的報錯一直這樣長篇大論的看的眼花~ - ~),但仔細閱讀就會發現大多數信息都是重複的。
真正可以定位錯誤原因的只有兩句:

  • ** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
  • Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning
    a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)

中文對照

  • ** 警告 **:由於默認包中的 @ComponentScan,你的應用文件可能不會被啓動
  • 無法識別 org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration 上的條件。由於沒有找到 org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseType 文件。
    確保你的配置不依賴該類。如果你這樣使用 @ComponentScanning 也會發生這種錯誤(例如:如果你誤將 @ComponentScanning 放置在默認包中)

問題解決

從錯誤描述可以明顯看出,@ComponentScan 註解是不允許放置在默認包中的。
我們只需要在默認包,也就是根目錄(一般情況下就是 src 目錄,具體視項目而定)下再新建一個包,然後將啓動類文件放入即可。



注意:目錄與包是兩個不同的概念

可能會遇到這樣的問題(如圖):
e1

啓動類 App.java 文件是與 java 包同級,位於 src 目錄下兩層了,爲什麼還會出現這個報錯?


這就是剛纔所說的目錄與包的不同,當前啓動類文件位於的是 src/main/java 目錄下,但卻依然位於項目的默認包下,因爲代碼中並沒有體現出聲明包的 package 語句


右鍵 java 文件夾,創建一個包,命名爲 application,將啓動類文件拖入,你會發現:

c1

這樣,啓動類文件 app.java 纔算是進入了 application 包下。脫離了默認包,

啓動項目:

2019-06-12 20:54:52.050  INFO 1788 --- [           main] com.App                                  : Started App in 2.341 seconds (JVM running for 3.408)
2019-06-12 20:56:54.735  INFO 1788 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2019-06-12 20:56:54.735  INFO 1788 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2019-06-12 20:56:54.754  INFO 1788 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms

啓動成功~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章