【发布时间】:2019-09-08 12:43:18
【问题描述】:
我想创建一个 Spring Boot 应用程序(打包为 WAR),它会根据部署的环境自动覆盖一些配置文件。我还希望属性文件在 WAR 之外。我使用 OS Centos 和 Tomcat 作为 Web 服务器。
我正在尝试关注 Vladimir Mitev 在以下问题 Similar question 中的回答。
为了实现这一点,我创建了这个 SPRING_CONFIG_ADDITIONAL_LOCATION 环境变量。然后在路径中我创建了一个 db.properties 文件,其中包含我想要覆盖的属性。
然后在 servlet 初始化器中我把这个配置:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class).properties("spring.config.name: db");
}
}
这是另一个需要的类:
@SpringBootApplication
public class MyApplication{
public static void main(String[] args) {
System.setProperty("spring.config.name", "db");
SpringApplication.run(MyApplication.class, args);
}
}
但是Spring Boot在初始化过程中找不到db.properties文件。
在official documentation 似乎我必须使用这个“spring.config.additional-location”才能实现我的目标,但我不知道如何。
【问题讨论】:
标签: java spring-boot tomcat configuration war