java 在多个上下文中找不到属性:属性占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16892752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
property not found with multiple context:property-placeholder
提问by Abby
I am using spring 3.1 with spring profiles to load the beans. In my app context file, I load the properties like :
我正在使用带有弹簧配置文件的 spring 3.1 来加载 bean。在我的应用程序上下文文件中,我加载了如下属性:
<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 configFactoryBeanuses the datasourceto 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 datasourcebefore resolving the property from the first context property placeholder. I may be wrong. Or maybe spring profile variable is not resolved properly.
我的分析是它试图datasource从第一个上下文属性占位符解析属性之前加载。我可能是错的。或者可能没有正确解析弹簧配置文件变量。
Can anyone please help me to fix this.
任何人都可以帮我解决这个问题。
Thanks Akki
谢谢阿基
回答by user3415190
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
PropertyPlaceholderConfigurerin conjunction with@Valueannotation and default value for placeholders syntax (ie${key:defaultValue}), only the firstPropertyPlaceholderConfigureris used. If this configurer does not contain the desired value, it falls back to@Valuedefault even if the secondPropertyPlaceholderConfigurercontains the value.Affects Version/s: 3.1.3
当使用 multiple
PropertyPlaceholderConfigurer与@Value注释和占位符语法(即${key:defaultValue})的默认值 结合使用时 ,仅使用第一个PropertyPlaceholderConfigurer。如果此配置器不包含所需的值,@Value即使第二个PropertyPlaceholderConfigurer包含该值,它也会回退到默认 值。影响版本:3.1.3
回答by Japan Trivedi
In my application I am using property-placeholder configurer in following way and it works very well. 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. :)
我认为这应该可以解决您的问题。:)
回答by Rostislav Matl
Each <context:property-placeholder> creates a new instance of PropertyPlaceholderConfigurer- it gets messy easily. You should have one such thing per application and on application level, not on libraries' one - that makes maintenance much easier.
每个<context:property-placeholder> 都会创建一个 PropertyPlaceholderConfigurer 的新实例- 它很容易变得混乱。您应该在每个应用程序和应用程序级别上拥有一个这样的东西,而不是在库的一个 - 这使得维护更容易。
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
回答by Kevin Bowersox
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.
由于您建议对配置文件的路径进行硬编码,请尝试使用标签上的配置文件属性来选择性地包含配置。
<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

