【问题标题】:How to call properties file from another properties file using Spring PropertyPlaceholderConfigurer?如何使用 Spring PropertyPlaceholderConfigurer 从另一个属性文件调用属性文件?
【发布时间】:2012-03-24 09:41:23
【问题描述】:

我正在使用 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 时,它无法加载属性。

【问题讨论】:

    标签: java spring properties


    【解决方案1】:

    BeanFactoryPostProcessorsPropertyPlaceholderConfigurer的执行顺序可以通过设置属性"order"来设置(见Ordered)。通过设置parentProperty的执行优先级高于child1Property可以保证parentProperty优先运行,配置${child1}的值。

    【讨论】:

    • 我尝试在 parentProperty 中设置 order="1" 参数,也尝试通过添加 depends-on="parentProperty" 但它仍然失败 ${child1}豆>
    • 再次查看您的配置,需要解析占位符 ${mypath} 才能加载 parentProperty。您将需要使用另一个 PropertyPlaceholderConfigurer 来解析此占位符(设置 systemsPropertiesMode 以使用系统属性)或以其他方式提供此属性文件的路径。
    • 当我运行我的应用程序时。我的 parentProperty 能够从 ${myfile} 中指定的文件中读取所有属性。我能够读取文件位置以外的属性。但我正在尝试读取包含文件位置的属性,它无法加载文件。它说资源 ${child1} 的 FileNotFoundException
    【解决方案2】:

    从类路径加载属性可能更容易,其中位置包含在类路径中,而不是在文件中,然后以下内容将加载所有属性文件。

    <bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true" />
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="locations">
            <list>
                <value>classpath*:*.properties</value>
            </list>
        </property>
    </bean>
    

    【讨论】:

    • 我的应用程序要求“不要将属性文件放在类路径中”。
    【解决方案3】:

    我首先加载了父属性文件,然后在系统环境变量中设置了特定的 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" />
    

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 2019-08-20
      • 2018-07-21
      • 2013-08-21
      • 2010-12-08
      • 1970-01-01
      • 2011-04-25
      • 2012-06-06
      • 1970-01-01
      相关资源
      最近更新 更多