【发布时间】:2020-05-03 06:23:16
【问题描述】:
在 Spring-Boot 2.2.0 和 Java8 项目中。
我的应用程序中有一些属性-(profileActive).properties(所以,在战争中)我想外部化为一个普通的配置文件(在战争之外)。这是因为这些属性每半年更改一次,将它们放在纯文本文件中更方便(我可以使用 sed 命令等)。
可变属性文件的位置应在属性mutableproperties.project.root
中的application-(profileActive).properties 中指定(它会根据环境而变化)我正在寻找一种解决方案,让整个项目中的所有 @Value 都像什么都没发生一样继续工作(重启后)。
我正在尝试使用以下类为一个文件加载这些属性:
@Configuration
public class MutableProperties {
@Value("${mutableproperties.project.root}")
private String mutablePropertiesRoot;
private String configFile(String type) {
StringBuffer file = new StringBuffer(mutablePropertiesRoot).append(File.pathSeparatorChar);
file.append(type).append(".properties");
return file.toString();
}
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurerDb() {
PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
properties.setLocation(new FileSystemResource(configFile("db")));
properties.setIgnoreResourceNotFound(false);
return properties;
}
}
问题是 mutableRoot 是 null (并且它在所有应用程序-(profileActive).properties 中)为:
mutableproperties.project.root=/etc/properties/boot/myproject
我尝试过使用静态 PropertySourcesPlaceholderConfigurer,但它不适合,因为文件名实际上是动态的。
我对解决问题的其他解决方案持开放态度,但对 JVM 的更改或跨多个类的操作不合适。这必须是一项可以在已经投入生产的环境中进行的手术更改
【问题讨论】:
-
您所引用的文件是否在类路径中可用?
-
应用程序-(activeProfile).properties,是的。可变属性文件不是。但问题是我什至无法读取位置
-
所以你想加载 /etc/properties/boot/myproject 下的所有文件,这应该是应用程序上下文的一部分?是这样吗?
-
是的。这就是我使用 PropertySourcesPlaceholderConfigurer 的原因。并且您所指的文件的位置是在运行时加载的
-
-Dspring.config.location 可以使用
标签: spring-boot properties-file