“当前目录”中的 Spring Boot 外部属性文件被忽略

cli*_*ell 1 java tomcat externalizing properties-file spring-boot

从手册:

\n\n
\n

24.3\xc2\xa0Application 属性文件 SpringApplication 将从以下位置的 application.properties 文件加载属性,并将它们添加到 Spring 环境中:

\n\n

当前目录的 /config 子目录。

\n\n

当前目录

\n\n

类路径 /config 包

\n\n

类路径根

\n
\n\n

它两次提到当前目录,但这实际上没有任何意义:

\n\n

我尝试将其放在项目的根目录中(即上面与和src的输出匹配的文件夹中),并且我尝试将其与 war 文件放在一起(即在)java.io.File( "." ).getCanonicalPath()System.getProperty("user.dir");build\\libs

\n\n

但唯一真正有效的放置位置是默认位置 ( src\\main\\resources)。

\n\n

那么“当前目录”是什么意思以及文件真正去哪里呢?

\n\n

我需要找到文件的正确外部位置,这样我就不必在应用程序中构建数据库凭据。

\n\n

指南说放入application.properties当前目录将起作用,我找到了将其放入的确切当前目录,但它仍然不起作用,我可以通过以下输出来验证:System.out.println(System.getProperty("spring.datasource.url"));null确实仅使用嵌入的属性文件。

\n

tan*_*an9 5

根据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/";
Run Code Online (Sandbox Code Playgroud)

file:./解析为启动 java 进程的工作目录。