【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

启动成功~~~

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