【问题标题】:property not found with multiple context:property-placeholder在多个上下文中找不到属性:属性占位符
【发布时间】:2013-05-29 08:50:43
【问题描述】:

我正在使用带有弹簧配置文件的 spring 3.1 来加载 bean。在我的应用上下文文件中,我加载如下属性:

<context:property-placeholder order="1"  location="classpath*:META-INF/spring/*_${spring.profiles.active}.properties" ignore-unresolvable="true"/>

然后我使用属性值来像加载数据源bean一样

<property name="driverClassName" value="${database.driverClassName}"/>

它工作正常。 当我添加更多属性占位符以便可以加载某些数据库表中的属性时,问题就开始了。

这使用由

加载的属性引用
<bean id="configFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
   <constructor-arg ref="globalSystemConfiguration"/>
</bean>

为了补充细节,这个configFactoryBean 使用datasource 从数据库中加载属性。

当我这样做时,我有以下异常:

java.lang.ClassNotFoundException: ${database.driverClassName}

我的分析是它试图在从第一个上下文属性占位符解析属性之前加载datasource。我可能错了。或者弹簧配置文件变量可能没有正确解析。

谁能帮我解决这个问题。

谢谢 阿奇

【问题讨论】:

  • 尝试将 order 设置为 0
  • 没有。它不起作用。我将第一个的 order 更改为 0,但仍然有相同的错误。
  • 哦,等等,它没有找到您的数据库驱动程序。您是否已将其添加到类路径中?
  • 是的。如果我删除其他两个 标签,它就可以工作。我认为它无法解析属性 ${database.driverClassName}

标签: java spring hibernate spring-mvc


【解决方案1】:

由于您建议硬编码配置文件的路径有效,请尝试使用标签上的配置文件属性来选择性地包含配置。

<beans profile="profileName">
    <context:property-placeholder  order="1"  location="classpath*:META-INF/spring/hardcoded.properties" ignore-unresolvable="true"/>
</beans>

<beans profile="profileName2">    
    <context:property-placeholder order="1"  location="classpath*:META-INF/spring/hardcoded.properties" ignore-unresolvable="true"/>
</beans>

请参阅这篇解释配置文件的文章:http://java.dzone.com/articles/using-spring-profiles-xml

【讨论】:

  • 如果我删除其他两个 标签,它就可以工作。我猜它无法解析属性
  • 其他两个属性占位符是否指定了顺序?
  • @user1517723 CommonsConfigurationFactoryBean 如何发挥作用?
  • 当我从数据库加载属性时,它会从 org.apache.commons.configuration.DatabaseConfiguration 创建一个属性对象。这在内部使用指定的数据源
  • @user1517723 我什至在任何地方的文档中都找不到那个东西,我确实找到了一个论坛帖子说它在 Spring 3.x 中不受支持:forum.springsource.org/archive/index.php/t-125884.html
【解决方案2】:

在我的应用程序中,我以下列方式使用属性占位符配置器,它运行良好。你可以试试。

<bean id="propertyConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations">
            <list>
                <value>classpath*:META-INF/spring/*_${spring.profiles.active}.properties</value>
            </list>
          </property>
    </bean>

我认为这应该可以解决您的问题。 :)

【讨论】:

    【解决方案3】:

    每个 创建一个 PropertyPlaceholderConfigurer 的新实例 - 它很容易变得混乱。您应该在每个应用程序和应用程序级别上拥有一个这样的东西,而不是在库的那个上 - 这使得维护变得更加容易。

    有关更多详细信息和如何应对它的建议,请查看此处: http://rostislav-matl.blogspot.cz/2013/06/resolving-properties-with-spring.html

    【讨论】:

    • 但是order 属性的引入解决了这个问题(而不是创建一个新的实例重用和维护一个属性源链)。目前jira.spring.io/browse/SPR-9989 已开通。
    【解决方案4】:

    这个关于多个属性占位符的错误可能与您的问题有关:https://jira.spring.io/browse/SPR-9989

    当使用多个PropertyPlaceholderConfigurer@Value 注释和占位符语法的默认值(即 ${key:defaultValue}),只有第一个 PropertyPlaceholderConfigurer 是 用过的。如果此配置器不包含所需的值,则它属于 回到@Value默认即使第二个 PropertyPlaceholderConfigurer 包含值。

    影响版本/s:3.1.3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-05
      • 2012-08-03
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      相关资源
      最近更新 更多