作为一个老Java,依然还是有很多问题是没有遇到过的,昨天遇到了一个问题,值得记录一下。
昨天在写一个demo用来测试接口,发现新创建的spring boot项目启动竟然报错了,报错内容大致如下:
2025-01-08 08:58:37.533 INFO 16856 --- [ main] o.Application : Starting Application using Java 1.8.0_202 on DESKTOP-PE1273U with PID 16856 (D:\IdeaProjectsTB\data-send\target\classes started by DELL in D:\IdeaProjectsTB\data-send)
2025-01-08 08:58:37.539 INFO 16856 --- [ main] o.Application : No active profile set, falling back to 1 default profile: "default"
2025-01-08 08:58:37.607 WARN 16856 --- [ main] ionWarningsApplicationContextInitializer :** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of 'org'.
2025-01-08 08:58:37.835 WARN 16856 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/DELL/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.7.18/spring-boot-autoconfigure-2.7.18.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth 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)
2025-01-08 08:58:37.841 INFO 16856 --- [ main] ConditionEvaluationReportLoggingListener :Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2025-01-08 08:58:37.856 ERROR 16856 --- [ main] o.s.b.SpringApplication : Application run failedorg.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/C:/Users/DELL/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.7.18/spring-boot-autoconfigure-2.7.18.jar!/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryConfigurations$PoolConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth 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.Application.main(Application.java:10) ~[classes/:?]
Caused by: java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.r2dbc.ConnectionFactoryConfigurations$PoolConfiguration due to io/r2dbc/spi/ValidationDepth 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)Caused by: java.lang.NoClassDefFoundError: io/r2dbc/spi/ValidationDepth
at java.lang.Class.getDeclaredMethods0(Native Method) ~[?:1.8.0_202]
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[?:1.8.0_202]
at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[?:1.8.0_202]
让我感到奇怪的是这个项目是新建的还没有添加任何内容,启动应该是正常的,可偏偏启动失败。
经过一番排查我看到了这行警告
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of 'org'.
猜测到可能是启动类的包名问题引起的?于是我修改了启动类的包名,改成了com就能启动成功了 。
经过一番搜索问题原因大概是下面这段话
许多第三方库也使用
org
作为它们的根包名,可能会无意中加载并注册一些不应该被包含在应用上下文中的类。例如,如果你的应用依赖了一些框架或库,这些库可能包含有@Component
,@Service
,@Repository
等注解的类,而这些类并不属于你的应用逻辑的一部分。这可能导致不可预见的行为,如重复定义 bean、命名冲突或其他难以调试的问题。