Spring Boot - 从WEB-INF /文件夹加载.properties文件

时间:2016-07-07 10:30:38

标签: spring-mvc spring-boot

我有一个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)文件之外,但在内部 - 只是不在类路径上,但直接在WEB-INF/文件夹中,通常是其他资源(页面,图像)。

3 个答案:

答案 0 :(得分:1)

正如我在上面提到的那样提问:

将此行放入application.properties

logging.config=file:log4j.xml

第二个选项是将系统变量传递给-Dlogging.config=file:log4j.xml

在这种情况下,它应该位于JAR文件之外的当前目录中。

对评论的反应

如果您使用的是WAR文件,则根本不使用您的主类。所以PropertySource注释在那里没有任何影响。

答案 1 :(得分:0)

如果.properties文件中包含.war。然后,您可以尝试以下操作(假设WEB-INF目录位于.war文件的根目录中。

@PropertySource("classpath:/WEB-INF/conf/application.properties")

答案 2 :(得分:0)

原来这个问题是由Spring Boot 1.4.0中的SpringBootTestContextBootstrapper的错误造成的.RC1:https://github.com/spring-projects/spring-boot/issues/6371

相关问题