24.3 Application property files SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:
A /config subdirectory of the current directory.
The current directory
A classpath /config package
The classpath root
它两次提到当前目录,但这真的没有任何意义:
我试着把它放在我的项目的根目录中(即 src
上面与 java.io.File( ".").getCanonicalPath()< 的输出相匹配的文件夹中
和 System.getProperty("user.dir");
),我尝试将它与 war 文件放在一起(即在 build\libs
中)
但唯一真正有效的放置位置是默认位置 (src\main\resources
)。
那么“当前目录”到底是什么意思,文件到底去了哪里?
我需要为文件找到正确的外部位置,这样我就不必在应用中构建数据库凭据。
指南说将 application.properties
放入当前目录会起作用,我找到了要放入的确切当前目录,但它仍然不起作用,我可以通过输出验证: System.out.println(System.getProperty("spring.datasource.url"));
为 null
它仅使用嵌入式属性文件输出正确的值.
最佳答案
根据 ConfigFileApplicationListener :
// Note the order is from least to most specific (last one wins)
private static final String DEFAULT_SEARCH_LOCATIONS =
"classpath:/,classpath:/config/,file:./,file:./config/";
file:./
解析到您启动 java 进程的工作目录。
关于java - "current directory"中的 Spring Boot 外部属性文件被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40894087/