繁体   English   中英

java 或 spring 中类路径的根目录可以有 package 名称吗?

[英]can root directory of a classpath in java or spring have a package name?

这似乎是一个简单的问题,但我无法让它工作我有一个简单的 spring 启动 java 项目。 java.srcDirs设置为src 我假设类路径现在也有src

我可以将代码文件直接放入src目录吗? 还是我必须创建一个子目录?

发生的事情是,如果我创建一个子目录demo ,那么我可以设置package demo; 在演示中的代码文件中。 但我无法为src目录中的文件设置任何 package 名称。 如果我将其留空或“”,我会收到错误消息

declared package "src" does not match the expected package ""

如果我完全省略 package 名称,那么 spring 引导错误会说

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


2022-10-01 13:17:51.010  WARN 7999 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/home/user/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.4/1848e5e8e474cc880a7708f03f750736665d4157/spring-boot-autoconfigure-2.7.4.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)
2022-10-01 13:17:51.021  INFO 7999 --- [           main] ConditionEvaluationReportLoggingListener :

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-10-01 13:17:51.036 ERROR 7999 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL [jar:file:/home/user/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot-autoconfigure/2.7.4/1848e5e8e474cc880a7708f03f750736665d4157/spring-boot-autoconfigure-2.7.4.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)

包应该放在 srcdir/classpath 的子目录中是 java 标准的一部分吗? 这里很困惑。

可以把下面的class放在源码根目录下。

package demo;

public class MyClass {
}

当你编译代码时,class MyClass 会被放到 demo 目录下的 output 目录下。

因为这有效,所以不推荐。 Package 结构和目录结构应该匹配。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM