我有一个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