【发布时间】:2016-07-07 10:30:37
【问题描述】:
我有一个 Spring Boot 1.3 应用程序(部署为 .war),它需要能够从以下位置读取 .properties 文件:
WEB-INF/application.properties(在类路径之外,但相对于应用根文件夹)
...相对于:
WEB-INF/classes/application.properties(在类路径中,自动加载)
在 Spring Boot 1.3 中起作用的是以下 @PropertySource 注释:
@SpringBootApplication
@PropertySource(value = {"WEB-INF/application.properties"})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
它正确地获取了相对于应用根目录的.properties 文件。但是,在更新到 Spring Boot 1.4.0.RC1 后,这将停止工作。
从那时起,我尝试了以下方法:
@PropertySource("classpath:../application.properties")
@PropertySource("file:WEB-INF/application.properties")
还有
spring.config.location=classpath:../
spring.config.location=file:src/main/webapp/WEB-INF/
spring.config.location=WEB-INF/application.properties
但加载 .properties 时运气不佳。
我通常会将.properties 文件放在类路径中,但在这种情况下,由于我们的部署在远程位置上工作的方式,这不是一个选项。
我也不想使用绝对路径,因为要支持多个客户将是一场噩梦。
编辑:为了清楚起见 - 我想阅读的 .properties 不在 JAR(在我的情况下 - WAR)文件之外,而是在内部 - 只是不在类路径上,而是直接在 @ 987654333@ 通常其他资源(页面、图像)所在的文件夹。
【问题讨论】:
-
有没有可能把它放到战争之外?它会在罐子外找到那些,但不确定战争。见:Externalized Configuration
-
不幸的是,
/WEB-INF/conf/application.properties位置是给定的。