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

Abb*_*bby 7 java spring hibernate spring-mvc

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

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

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

<property name="driverClassName" value="${database.driverClassName}"/>
Run Code Online (Sandbox Code Playgroud)

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

这使用加载的属性引用

<bean id="configFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
   <constructor-arg ref="globalSystemConfiguration"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

要添加到详细信息,这configFactoryBean将使用datasource从数据库加载属性.

当我这样做时,我有以下例外:

java.lang.ClassNotFoundException: ${database.driverClassName}
Run Code Online (Sandbox Code Playgroud)

我的分析是它试图datasource在从第一个上下文属性占位符解析属性之前加载.我可能错了.或者弹簧轮廓变量未正确解析.

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

谢谢Akki

小智 11

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

当将多个PropertyPlaceholderConfigurer@Value注释和默认值结合使用 占位符语法(即 ${key:defaultValue})时,仅使用第一个PropertyPlaceholderConfigurer.如果此配置器不包含所需的值,则@Value即使第二个PropertyPlaceholderConfigurer包含该值,它也会回退到默认 值.

影响版本:3.1.3


Kev*_*sox 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>
Run Code Online (Sandbox Code Playgroud)

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