java - 如何使用 Spring PropertyPlaceholderConfigurer 从另一个属性文件调用属性文件?

标签 java spring properties

我正在使用 Spring 开发命令行 Java 应用程序。我有多个属性文件存储在不同的位置,还有一个属性文件包含所有这些属性的路径。我正在使用 PropertyPlaceholderConfigurer 来读取包含不同属性文件位置的属性。我不确定处理多个属性的最佳方法。

应用程序的工作方式如下:我将使用 JVM 命令 -Dmypath=parent.properties 传递第一个属性文件的路径。属性文件将如下所示:

child1=/location1/child1.properties

child2=/location2/child2.properties

等等

我的父属性配置如下所示:

<bean id="parentProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>                
            <value>${mypath}</value>
        </list>
    </property>
</bean>

child1 配置如下:

<bean id="child1Property" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>                
            <value>${child1}</value>
        </list>
    </property>
</bean>

现在,当我调用 child1 时,它无法加载属性。

最佳答案

我首先加载父属性文件,然后在系统环境变量中设置特定的child1和child2变量并从系统环境变量加载。并且工作正常。

代码:

<context:property-placeholder location="file:${mypath}/*.properties" ignore-unresolvable="true" />

<bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" value="#{@systemProperties}" />
        <property name="targetMethod" value="putAll" />
        <property name="arguments">
            <!-- The new Properties -->
            <util:properties>
                <prop key="LOG_LOCATION">${log.location}</prop>
                <prop key="child1">${child1}</prop>
            </util:properties>
        </property>
    </bean>

<context:property-placeholder location="file:#{systemProperties['child1']}/*.sql" ignore-unresolvable="true" />

关于java - 如何使用 Spring PropertyPlaceholderConfigurer 从另一个属性文件调用属性文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591701/

相关文章:

java - Java 中 HTTP 响应的返回值

java - Eclipse Kepler for OS X Mavericks 请求 Java SE 6

spring - Spring MVC中如何通过返回自定义错误页面来全局处理404异常?

java - 实现 Spring Web 服务的更好方法

javascript - 是否需要将 hasOwnProperty 与 Object.keys 一起使用?

java - 公共(public)配置 - JNDIConfiguration - 如何?

java - 即使JDK包含JRE,还需要JRE吗?

java - Windows 任务栏高度/宽度

java - 你能用 Liquibase 初始化 Spring Batch 元数据表吗?

qt4 - 有没有办法在 QObject 中的属性更改时收到通知?