PropertyPlaceholderConfigurer和.properties文件中的环境变量 [英] PropertyPlaceholderConfigurer and environment variables in .properties files
问题描述
property.name = $ {env.SYSTEM_PROPERTY}
我收到以下错误:
code> org.springframework.beans.factory.BeanDefinitionStoreException:类路径资源[com / example / applicationContext.xml]中定义的名称为beanName的无效bean定义:无法解析占位符env.SYSTEM_PROPERTY
而占位符配置器定义为
code>< bean class =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer>
< property name =locationvalue =classpath:com / example / application.properties/>
< / bean>
任何想法如何使property.name解释为环境变量(而不是占位符)?
最好的问候,Dmitriy。
我可能会改变解决方案:直接注入系统属性,而不是注入引用系统属性的属性
例如
@Value(#{systemProperties ['JAVA_MY_ENV']})
private String myVar;
或
< property name =myVarvalue =#{systemProperties ['JAVA_MY_ENV']}/>
我使用这样的属性占位符配置器
< bean id =propertyConfigurerclass =org.springframework.beans.factory.config.PropertyPlaceholderConfigurer>
< property name =locations>
< list>
< value> classpath:someprops.properties< / value>
< / list>
< / property>
< property name =ignoreResourceNotFoundvalue =true/>
< property name =searchSystemEnvironmentvalue =true/>
< property name =systemPropertiesModeNamevalue =SYSTEM_PROPERTIES_MODE_OVERRIDE/>
您还必须记住使用
$将参数传递到程序中b $ b
-DJAVA_MY_ENV = xyz
当您运行生产版本时,您可以传递一件事,当您运行测试另一件事。
另外我经常做的是这样的:
< property name =locations>
< list>
< value> classpath:someprops.properties< / value>
< value> classpath:someprops- {environment} .properties< / value>
< / list>
< / property>
环境是prod / stage / test / int / ci / local(每个环境1个 - 你可以现在只有2或3)。您可以将环境变量传递给程序。任何属性应该是相同的,无论它的生产/运行在你的本地pc /测试将在someprops.properties属性文件。任何特定于环境的方式/运行方式将在更具体的文件中(您应该将其放在someprops.properties文件以及默认情况下,除非重写机制)
例如classpath中的
:someprops.properties
url = www.mysite.com
在classpath中:someprops-local.properties
url = localhost
通过使用这个基本思想,您可以将测试和程序的正常运行属性分开一个干净的方式。
I have a Spring application-context.xml with PropertyPlaceholderConfigurer to get properties' values from .properties file. Main and test source folders have separate .properties file. The issue is that I need to use environment variables in .properties file. But when I do it in the following way:
property.name=${env.SYSTEM_PROPERTY}
I'm getting the following error:
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'
while placeholder configurer defined as
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:com/example/application.properties"/> </bean>
Any ideas how-to make property.name be interpreted as environment variable (and not as placeholder)?
Best regards, Dmitriy.
解决方案I'd probably change the solution completely: inject the system property directly, as opposed to injecting the property which refers to a system property
E.g.
@Value("#{ systemProperties['JAVA_MY_ENV'] }") private String myVar;
or
<property name ="myVar" value="#{systemProperties['JAVA_MY_ENV']}"/>
I use a property placeholder configurer like this
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:someprops.properties</value> </list> </property> <property name="ignoreResourceNotFound" value="true" /> <property name="searchSystemEnvironment" value="true" /> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
You must also remember to pass the parameter into the program using
-DJAVA_MY_ENV=xyz
This way when you run the production version you can pass one thing and when you are running tests another.
Also what I often what I do is something like this:
<property name="locations"> <list> <value>classpath:someprops.properties</value> <value>classpath:someprops-{environment}.properties</value> </list> </property>
where environment is prod/stage/test/int/ci/local (1 per environment - you may only have 2 or 3 for now). You can pass the environment variable to the program. Any properties which should be the same regardless of if its production/running on your local pc/tests would be in the someprops.properties property file. Any ones specific to the environment/way its being run as will go in the more specific file (you should put it in the someprops.properties file as well as a default unless overridden mechanism)
E.g. in classpath:someprops.properties
url=www.mysite.com
in classpath:someprops-local.properties
url=localhost
By using this basic idea you can separate tests and the program's normal running properties in a clean manner.
这篇关于PropertyPlaceholderConfigurer和.properties文件中的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!