[英]property not found with multiple context:property-placeholder
I am using spring 3.1 with spring profiles to load the beans. 我正在使用带有弹簧轮廓的spring 3.1来加载bean。 In my app context file, I load the properties like :
在我的应用上下文文件中,我加载如下属性:
<context:property-placeholder order="1" location="classpath*:META-INF/spring/*_${spring.profiles.active}.properties" ignore-unresolvable="true"/>
And then I use the property value to load the data source bean like 然后我使用属性值来加载数据源bean,例如
<property name="driverClassName" value="${database.driverClassName}"/>
It works fine. 工作正常。 The problem starts when I add a couple of more property placeholders so that properties from some database tables can be loaded.
当我添加几个其他属性占位符以便可以从某些数据库表加载属性时,问题就开始了。
This uses a properties reference loaded by 这使用由加载的属性引用
<bean id="configFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
<constructor-arg ref="globalSystemConfiguration"/>
</bean>
To add to the details, this configFactoryBean
uses the datasource
to load the properties from the database. 为了增加细节,此
configFactoryBean
使用datasource
从数据库加载属性。
When I do this, I have the following exception: 当我这样做时,我有以下异常:
java.lang.ClassNotFoundException: ${database.driverClassName}
My analysis is that its trying to load the datasource
before resolving the property from the first context property placeholder. 我的分析是,它试图在从第一个上下文属性占位符解析属性之前加载
datasource
。 I may be wrong. 我可能是错的。 Or maybe spring profile variable is not resolved properly.
或弹簧轮廓变量未正确解析。
Can anyone please help me to fix this. 谁能帮我解决这个问题。
Thanks Akki 谢谢Akki
This bug about multiple property placeholders might relate to your problem: https://jira.spring.io/browse/SPR-9989 关于多个属性占位符的此错误可能与您的问题有关: https : //jira.spring.io/browse/SPR-9989
When using multiple
PropertyPlaceholderConfigurer
in conjunction with@Value
annotation and default value for placeholders syntax (ie${key:defaultValue}
), only the firstPropertyPlaceholderConfigurer
is used.当使用多个
PropertyPlaceholderConfigurer
以及@Value
注释和占位符语法的默认值(即${key:defaultValue}
)时,仅使用第一个PropertyPlaceholderConfigurer
。 If this configurer does not contain the desired value, it falls back to@Value
default even if the secondPropertyPlaceholderConfigurer
contains the value.如果此配置程序不包含所需的值,则即使第二个
PropertyPlaceholderConfigurer
包含该值,它也会回@Value
默认值@Value
。Affects Version/s: 3.1.3
影响版本:3.1.3
In my application I am using property-placeholder configurer in following way and it works very well. 在我的应用程序中,我以以下方式使用property-placeholder configurer,它工作得很好。 You can try that.
你可以试试看。
<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>
I think this should resolve your problem. 我认为这应该可以解决您的问题。 :)
:)
Each <context:property-placeholder> creates a new instance of PropertyPlaceholderConfigurer - it gets messy easily. 每个<context:property-placeholder>都会创建一个PropertyPlaceholderConfigurer的新实例 -容易弄乱。 You should have one such thing per application and on application level, not on libraries' one - that makes maintenance much easier.
每个应用程序和应用程序级别都应该有一个这样的东西,而不是库的一个-这样会使维护容易得多。
For more details and a suggestion how to cope with it look here: http://rostislav-matl.blogspot.cz/2013/06/resolving-properties-with-spring.html 有关更多细节和建议,请参见: http : //rostislav-matl.blogspot.cz/2013/06/resolving-properties-with-spring.html
Since you have suggested hardcoding the path to the configuration file works, try using the profiles attribute on the tag to selectively include the configuration. 由于已经建议对配置文件的路径进行硬编码,因此,请尝试使用标签上的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>
See this article explaining profiles: http://java.dzone.com/articles/using-spring-profiles-xml 请参阅这篇说明配置文件的文章: http : //java.dzone.com/articles/using-spring-profiles-xml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.