Spring 从 application.properties 启动 属性 未在 @Configuration class 中的 @Value 中加载

Spring Boot property from application.properties not loaded in @Value in @Configuration class

在 Spring-Boot 2.2.0 和 Java8 项目中。

我的应用程序中有一些属性-(profileActive).properties(因此,在 war 中)我想外部化到一个普通配置文件(在 war 之外)。这是因为这些属性每半年更改一次,将它们放在纯文本文件中更方便(我可以使用 sed 命令等)。

可变属性文件的位置应在 属性 mutableproperties.project.root[= 中的 application-(profileActive).properties(根据环境变化)中指定34=]

我正在寻找一个解决方案,在整个项目中,所有 @Value 都可以像什么都没发生一样(重启后)继续工作。

我正在尝试使用下面的 class 为一个文件加载这些属性:

@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 的更改或跨多个 classes 的操作并不合适。这必须是一个可以在已经投入生产的环境中运行的手术式更改

我终于放弃了配置中的@Value。

解决方案是在 maven 配置文件中包含 属性(因此,属性 不是硬编码的,它在 pom 中),让 maven 在在此 属性 上设置的路径,使用 org.codehaus.mojo.properties-maven-plugin.

有了这个:

  1. 我正在生成一个动态属性文件(在 eclipse 中找不到,只能在 war 中找到),由 maven 包含在 war
  2. 里面有个属性,可编辑属性文件所在的路径
  3. 我现在可以从 PropertySourcesPlaceholderConfigurer 加载所有属性 是的,属性文件的名称是硬编码的,就像 application.properties 一样,这不是问题

私有静态最终字符串FROMPOM_PROPERTIES_LOCATION = "/frompom.properties";

私有静态最终字符串MUTABLES_ROOT_PROPERTY = "mutable.root";

private String mutablesLocation() throws ...{
    Resource resource = new ClassPathResource("/frompom.properties");
    return PropertiesLoaderUtils.loadProperties(resource).getProperty("mutable.root");
}

private PropertySourcesPlaceholderConfigurer configurer(String type) {
    PropertySourcesPlaceholderConfigurer properties = new PropertySourcesPlaceholderConfigurer();
    File file = new File("/" + mutablesLocation() + "mutable.properties");
    properties.setLocation(new FileSystemResource(file));
    return properties;
}

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0.0</version>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>write-project-properties</goal>
            </goals>
            <configuration>
                <outputFile>${project.build.outputDirectory}\frompom.properties</outputFile>
            </configuration>
        </execution>
    </executions>
</plugin>