找不到多个上下文的属性:property-placeholder

时间:2013-06-03 08:31:22

标签: java spring hibernate spring-mvc

我使用带有弹簧轮廓的弹簧3.1来加载豆子。在我的应用程序上下文文件中,我加载了以下属性:

<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。我可能错了。或者弹簧轮廓变量未正确解析。

任何人都可以帮我解决这个问题。

由于 Akki

4 个答案:

答案 0 :(得分:11)

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

  

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

     

影响版本:3.1.3

答案 1 :(得分:4)

在我的应用程序中,我按照以下方式使用property-placeholder configurer,它运行良好。你可以试试。

<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>

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

答案 2 :(得分:4)

每个&lt; context:property-placeholder&gt;创建一个PropertyPlaceholderConfigurer的新实例 - 它很容易弄乱。每个应用程序和应用程序级别应该有一个这样的东西,而不是库的一个 - 这使维护更容易。

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

答案 3 :(得分:1)

由于您已建议对配置文件的路径进行硬编码,请尝试使用标记上的profiles属性选择性地包含配置。

<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

相关问题