java - "current directory"中的 Spring Boot 外部属性文件被忽略

标签 java tomcat spring-boot properties-file externalizing

from the manual:

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/

相关文章:

java - 似乎无法正确打印我的嵌套循环

java - Spring Data Cassandra 执行的日志查询

tomcat - 监控 catalina.out [多行日志文件]

java - 使用递归父子查询特征而不是Java特征

spring-boot - 基于线程池状态扩展Spring Boot App

java - org.springframework.beans.factory.UnsatisfiedDependencyException : Error creating bean with name.无法让我的springboot应用程序运行

Java:在多线程和事务环境中运行 DAO 类

java - 用java加载最简单的静态模型格式?

java - JMS 统计主题域中未决消息的数量

java - 是否可以使用框架 spring 5 运行 java 10?