java - Spring-如何使用PropertyPlaceholderConfigurer动态加载文件

标签 java spring properties

我有 Spring Framework 的下一个属性文件

配置.属性
有内容

environment=devel //posible values: devel, testing, prod

并使用之前的环境属性,选择以下一些文件进行动态加载

config-service1-devel.properties
config-service1-testing.properties
config-service1-prod.properties
config-serviceN-devel.properties
config-serviceN-testing.properties
config-serviceN-prod.properties

然后,使用 spring 我想加载属性,我解决了加载第一个属性文件的问题,但我不明白如何使用表达式语言来完成依赖属性的值。

<bean id="MainApplicationProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location"
        value="file://#{systemProperties['jboss.server.home.dir']}/conf/services.properties" />

    <property name="placeholderPrefix" value="$mainProperty{" />
    <property name="placeholderSuffix" value="}" />
</bean>
<bean id="SecondApplicationProperties"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
    depends-on="MainApplicationProperties">

    <property name="locations">
        <list>
            <value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceOne/service1-$mainProperty{environment}.properties</value>
            <value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceTwo/service2-$mainProperty{environment}.properties</value>
            <value>file://#{systemProperties['jboss.server.home.dir']}/conf/serviceN/serviceN-$mainProperty{environment}.properties</value>
        </list>
    </property>

</bean>

错误输出是下一个,

java.io.FileNotFoundException: /..../conf/serviceOne/service1-$mainProperty{environment}.properties (No such file or directory)

我的观点是,值(value)没有被取代

帮帮我,谢谢

最佳答案

问题是当BeanFactoryPostProcessors开始被调用时,它们已经被实例化了。因此,即使您第一个 PropertyPlaceholderConfigurer 修改了第二个 PropertyPlaceholderConfigurer 的 bean 定义,它也没有任何效果,因为两个 bean 都已实例化。

关于java - Spring-如何使用PropertyPlaceholderConfigurer动态加载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16993311/

相关文章:

java - 使用 Java AWT 将图像添加到面板

java - 扩展 Java 第三方 API

java - 如何防止在catalina.out中重复记录到tomcat控制台?

java - 基于 Spring 代码的配置 - IllegalArgumentException : A ServletContext is required

c# - 如何通过名称查找访问对象的属性?

java - 字母数字字段的字符串增量是因为 JPA 不起作用

java - 使用Springframework Page对象时不向前端暴露具有复合主键的实体的路径

java - Spring Vaadin 主题

java - HashMap ,无序键

java - 为什么 Spring Boot 的资源优先级会忽略我的外部属性文件?