[英]property not found with multiple context:property-placeholder
我正在使用带有弹簧轮廓的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
。 我可能是错的。 或弹簧轮廓变量未正确解析。
谁能帮我解决这个问题。
谢谢Akki
关于多个属性占位符的此错误可能与您的问题有关: https : //jira.spring.io/browse/SPR-9989
当使用多个
PropertyPlaceholderConfigurer
以及@Value
注释和占位符语法的默认值(即${key:defaultValue}
)时,仅使用第一个PropertyPlaceholderConfigurer
。 如果此配置程序不包含所需的值,则即使第二个PropertyPlaceholderConfigurer
包含该值,它也会回@Value
默认值@Value
。影响版本:3.1.3
在我的应用程序中,我以以下方式使用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>
我认为这应该可以解决您的问题。 :)
每个<context:property-placeholder>都会创建一个PropertyPlaceholderConfigurer的新实例 -容易弄乱。 每个应用程序和应用程序级别都应该有一个这样的东西,而不是库的一个-这样会使维护容易得多。
有关更多细节和建议,请参见: http : //rostislav-matl.blogspot.cz/2013/06/resolving-properties-with-spring.html
由于已经建议对配置文件的路径进行硬编码,因此,请尝试使用标签上的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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.