我有一个带有 PropertyPlaceholderConfigurer 的 Spring application-context.xml,用于从 .properties 文件中获取属性值.主要和测试源文件夹具有单独的 .properties 文件.问题是我需要在 .properties 文件中使用环境变量.但是当我按照以下方式进行操作时:
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}
我收到以下错误:
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 被解释为环境变量(而不是占位符)?
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
例如
@Value("#{ systemProperties['JAVA_MY_ENV'] }")
private String myVar;
或
<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" />
您还必须记住使用
-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>
其中环境是 prod/stage/test/int/ci/local(每个环境 1 个 - 您现在可能只有 2 或 3 个).您可以将环境变量传递给程序.无论其在本地 pc/tests 上的生产/运行是否在 someprops.properties 属性文件中,任何应该相同的属性.任何特定于环境/运行方式的文件都将放入更具体的文件中(您应该将其放入 someprops.properties 文件以及默认值,除非被覆盖机制)
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)
例如在类路径中:someprops.properties
E.g. in classpath:someprops.properties
url=www.mysite.com
在类路径中:someprops-local.properties
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.
这篇关于.properties 文件中的 PropertyPlaceholderConfigurer 和环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!