未从@Configuration类的@Value中加载application.properties的Spring Boot属性

问题描述 投票:0回答:1

在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进行更改或对许多类进行操作。这必须是可以在已经生产的环境中工作的手术更改

在Spring-Boot 2.2.0和Java8项目中。我的应用程序中有一些属性-(profileActive).properties(因此,在战争中),我想外部化到一个普通的配置文件(战争之外)。这是...

spring-boot properties-file
1个回答
0
投票

我终于放弃了Configuration中的@Value。

© www.soinside.com 2019 - 2024. All rights reserved.