簡體   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