代码之家  ›  专栏  ›  技术社区  ›  black666

在多个项目/模块中使用多个属性文件(通过PropertyPlaceHolderConfigure)

  •  96
  • black666  · 技术社区  · 14 年前

    我们目前正在编写一个分为多个项目/模块的应用程序。例如,让我们学习以下模块:

    • 我的应用程序jabber

    每个模块都有自己的Spring上下文xml文件。对于DAO模块,我有一个propertyplaceholderconfigure,它读取带有必要db连接参数的属性文件。在jabber模块中,我还有一个用于jabber连接属性的PropertyPlaceHolderConfigurer。

    我有点理解问题是什么,但我真的不知道一个解决方案-或者我的用例的最佳实践。

    如何配置每个模块,使每个模块都能够加载自己的属性文件?现在,我已经将propertyplaceholderconfigure从单独的上下文文件中移出,并将它们合并到主应用程序的上下文中(使用单个propertyplaceholderconfigure加载所有属性文件)。但这很糟糕,因为现在每个使用dao模块的人都必须知道,他们的上下文中需要一个PropertyPlaceHolderConfigurer。。dao模块中的集成测试也失败了。

    5 回复  |  直到 12 年前
        1
  •  184
  •   Joshua Taylor    9 年前

    如果您确保在涉及的每个上下文中,每个占位符都忽略了无法解析的键,那么这两种方法都有效。例如:

    <context:property-placeholder
    location="classpath:dao.properties,
              classpath:services.properties,
              classpath:user.properties"
    ignore-unresolvable="true"/>
    

        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:dao.properties</value>
                    <value>classpath:services.properties</value>
                    <value>classpath:user.properties</value>
                </list>
            </property> 
            <property name="ignoreUnresolvablePlaceholders" value="true"/>
        </bean>
    
        2
  •  18
  •   Raul Rene    11 年前

    ignore-unresolvable 物业不适合我,我也不知道为什么。

    location="file:${CATALINA_HOME}/conf/db-override.properties" ignore-unresolvable="true" 在这种情况下不起作用。

    ignore-resource-not-found="true"
    

        3
  •  8
  •   earldouglas    14 年前

    <context:property-placeholder /> 元素,而不是显式声明多个PropertiesPlaceholderConfigurer bean。

        4
  •  4
  •   Stephen C    14 年前

    这个 PropertiesPlaceholderConfigurer bean有一个称为“propertiesArray”的可选属性。使用此属性而不是“properties”属性,并使用 <array> 属性引用。

        5
  •  2
  •   onurbaysan    8 年前

    我尝试了下面的解决方案,它在我的机器上工作。

    <context:property-placeholder location="classpath*:connection.properties" ignore-unresolvable="true" order="1" />
    
    <context:property-placeholder location="classpath*:general.properties" order="2"/>
    

    如果系统中存在多个元件 在Spring上下文中,有一些最佳实践应该 跟着:

    需要指定order属性来修复 这些由Spring所有属性占位符减去最后一个 一个(最高阶)应该有 ignore-unresolvable=”true” 允许 解决机制,以传递给他人的上下文中没有 引发异常

    资料来源: http://www.baeldung.com/2012/02/06/properties-with-spring/