我有一個Spring application-context.xml,使用PropertyPlaceholderConfigurer從.properties文件獲取屬性值。 Main和測試源文件夾具有單獨的.properties文件。問題是我需要在.properties文件中使用環境變量。但是,當我這樣做以下列方式:.properties文件中的PropertyPlaceholderConfigurer和環境變量
property.name=${env.SYSTEM_PROPERTY}
,我發現了以下錯誤:
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'beanName' defined in class path resource [com/example/applicationContext.xml]: Could not resolve placeholder 'env.SYSTEM_PROPERTY'
而佔位符配置者定義爲
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:com/example/application.properties"/>
</bean>
任何想法如何,使property.name被解釋爲環境變量(而不是佔位符)?
最好的問候,德米特里。
這是相當不錯的。然而,一個問題是,忘記定義運行時變量會產生棘手的運行時錯誤,比如找不到config_xxx文件等。爲了改善這種情況,我註冊了一個實現瞭如下特殊彈簧接口的bean:class EnvironmentReporter實現了PriorityOrdered,BeanFactoryPostProcessor,EnvironmentAware {...} environment.getProperty允許我檢查期望的屬性並在任何其他bean獲取之前乾淨地失敗創建。 – Federico 2013-08-16 13:00:21