cli*_*ell 1 java tomcat externalizing properties-file spring-boot
\n\n\n\n\n24.3\xc2\xa0Application 属性文件 SpringApplication 将从以下位置的 application.properties 文件加载属性,并将它们添加到 Spring 环境中:
\n\n当前目录的 /config 子目录。
\n\n当前目录
\n\n类路径 /config 包
\n\n类路径根
\n
它两次提到当前目录,但这实际上没有任何意义:
\n\n我尝试将其放在项目的根目录中(即上面与和src的输出匹配的文件夹中),并且我尝试将其与 war 文件放在一起(即在)java.io.File( "." ).getCanonicalPath()System.getProperty("user.dir");build\\libs
但唯一真正有效的放置位置是默认位置 ( src\\main\\resources)。
那么“当前目录”是什么意思以及文件真正去哪里呢?
\n\n我需要找到文件的正确外部位置,这样我就不必在应用程序中构建数据库凭据。
\n\n指南说放入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/";
Run Code Online (Sandbox Code Playgroud)
file:./解析为启动 java 进程的工作目录。
| 归档时间: |
|
| 查看次数: |
22571 次 |
| 最近记录: |