【问题标题】:Spring Boot property from application.properties not loaded in @Value in @Configuration class来自 application.properties 的 Spring Boot 属性未加载到 @Configuration 类的 @Value 中
【发布时间】: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


【解决方案1】:

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

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

有了这个:

  1. 我正在生成一个动态属性文件(在 Eclipse 中找不到,仅在战争中),由 maven 包含在战争中
  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>

【讨论】:

    猜你喜欢
    • 2017-09-19
    • 2016-03-04
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多