Java 如何在Spring applicationContext中读取系统环境变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3965446/
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
how to read System environment variable in Spring applicationContext
提问by jai
How to read the system environment variable in the application context?
如何在应用上下文中读取系统环境变量?
I want something like :
我想要类似的东西:
<util:properties id="dbProperties"
location="classpath:config_DEV/db.properties" />
or
或者
<util:properties id="dbProperties"
location="classpath:config_QA/db.properties" />
depending on the environement.
取决于环境。
Can I have something like this in my application Context?
我的应用程序上下文中可以有这样的东西吗?
<util:properties id="dbProperties"
location="classpath:config_${systemProperties.env}/db.properties" />
where the actual val is set based on the SYSTEM ENVIRONMENT VARIABLE
其中实际 val 是根据系统环境变量设置的
I'm using Spring 3.0
我正在使用 Spring 3.0
采纳答案by Bozho
Check this article. It gives you several ways to do this, via the PropertyPlaceholderConfigurer
which supports external properties (via the systemPropertiesMode
property).
检查这篇文章。它为您提供了几种方法来做到这一点,通过PropertyPlaceholderConfigurer
它支持外部属性(通过systemPropertiesMode
属性)。
回答by Istao
Yes, you can do <property name="defaultLocale" value="#{ systemProperties['user.region']}"/>
for instance.
是的,<property name="defaultLocale" value="#{ systemProperties['user.region']}"/>
例如,您可以这样做。
The variable systemPropertiesis predefined, see 6.4.1 XML based configuration.
变量systemProperties是预定义的,请参阅6.4.1 基于 XML 的配置。
回答by amra
You are close :o) Spring 3.0 adds Spring Expression Language. You can use
你很接近 :o) Spring 3.0 添加了Spring Expression Language。您可以使用
<util:properties id="dbProperties"
location="classpath:config_#{systemProperties['env']}/db.properties" />
Combined with java ... -Denv=QA
should solve your problem.
结合使用java ... -Denv=QA
应该可以解决您的问题。
Note also a comment by @yiling:
另请注意@yiling 的评论:
In order to access system environment variable, that is OS level variables as amoe commented, we can simply use "systemEnvironment" instead of "systemProperties" in that EL. Like
#{systemEnvironment['ENV_VARIABLE_NAME']}
为了访问系统环境变量,即 amoe 评论的操作系统级别变量,我们可以简单地在该 EL 中使用“systemEnvironment”而不是“systemProperties”。喜欢
#{systemEnvironment['ENV_VARIABLE_NAME']}
回答by Brad Parks
In your bean definition, make sure to include "searchSystemEnvironment" and set it to "true". And if you're using it to build a path to a file, specify it as a file:/// url.
在您的 bean 定义中,确保包含“searchSystemEnvironment”并将其设置为“true”。如果您使用它来构建文件路径,请将其指定为 file:/// url。
So for example, if you have a config file located in
例如,如果您有一个配置文件位于
/testapp/config/my.app.config.properties
then set an environment variable like so:
然后像这样设置一个环境变量:
MY_ENV_VAR_PATH=/testapp/config
and your app can load the file using a bean definition like this:
并且您的应用程序可以使用这样的 bean 定义加载文件:
e.g.
例如
<bean class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true" />
<property name="searchContextAttributes" value="true" />
<property name="contextOverride" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>file:///${MY_ENV_VAR_PATH}/my.app.config.properties</value>
</list>
</property>
</bean>
回答by eis
For my use case, I needed to access just the system properties, but provide default values in case they are undefined.
对于我的用例,我只需要访问系统属性,但在未定义的情况下提供默认值。
This is how you do it:
这是你如何做到的:
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="searchSystemEnvironment" value="true" />
</bean>
<bean id="myBean" class="path.to.my.BeanClass">
<!-- can be overridden with -Dtest.target.host=http://whatever.com -->
<constructor-arg value="${test.target.host:http://localhost:18888}"/>
</bean>
回答by Mikus
Using Spring EL you can eis examplewrite as follows
使用 Spring EL,您可以编写如下示例
<bean id="myBean" class="path.to.my.BeanClass">
<!-- can be overridden with -Dtest.target.host=http://whatever.com -->
<constructor-arg value="#{systemProperties['test.target.host'] ?: 'http://localhost:18888'}"/>
</bean>
回答by Atit Shah
This is how you do it:
这是你如何做到的:
<bean id="systemPrereqs" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" scope="prototype">
<property name="targetObject" value="#{@systemProperties}" />
<property name="targetMethod" value="putAll" />
<property name="arguments">
<util:properties>
<prop key="deployment.env">dev</prop>
</util:properties>
</property>
</bean>
But remember spring gets loaded first and then it will load this bean MethodInvokingFactoryBean. So if you are trying to use this for your test case then make sure that you use depends-on. For e.g. in this case
但请记住,spring 首先被加载,然后它会加载这个 bean MethodInvokingFactoryBean。因此,如果您尝试将其用于测试用例,请确保使用依赖项。例如在这种情况下
In case you are using it for your main class better to set this property using your pom.xml as
如果您将它用于主类,最好使用 pom.xml 将此属性设置为
<systemProperty>
<name>deployment.env</name>
<value>dev</value>
</systemProperty>
回答by Dariusz
Nowadays you can put
现在你可以把
@Autowired
private Environment environment;
in your @Component
, @Bean
, etc., and then access the properties through the Environment
class:
在您的@Component
,@Bean
等中,然后通过Environment
类访问属性:
environment.getProperty("myProp");
For a single propertyin a @Bean
对于单一属性的@Bean
@Value("${my.another.property:123}") // value after ':' is the default
Integer property;
Another wayare the handy @ConfigurationProperties
beans:
另一种方法是方便的@ConfigurationProperties
bean:
@ConfigurationProperties(prefix="my.properties.prefix")
public class MyProperties {
// value from my.properties.prefix.myProperty will be bound to this variable
String myProperty;
// and this will even throw a startup exception if the property is not found
@javax.validation.constraints.NotNull
String myRequiredProperty;
//getters
}
@Component
public class MyOtherBean {
@Autowired
MyProperties myProperties;
}
Note: Just remember to restart eclipse after setting a new environment variable
注意:设置新环境变量后记得重启eclipse
回答by Sunil
You can mention your variable attributes in a property file and define environment specific property files like local.properties, production.propertied etc.
您可以在属性文件中提及您的变量属性,并定义特定于环境的属性文件,如 local.properties、production.properied 等。
Now based on the environment, one of these property file can be read in one the listeners invoked at startup, like the ServletContextListener.
现在基于环境,可以在启动时调用的侦听器之一中读取这些属性文件之一,例如 ServletContextListener。
The property file will contain the the environment specific values for various keys.
属性文件将包含各种键的环境特定值。
Sample "local.propeties"
示例“local.propeties”
db.logsDataSource.url=jdbc:mysql://localhost:3306/logs
db.logsDataSource.username=root
db.logsDataSource.password=root
db.dataSource.url=jdbc:mysql://localhost:3306/main
db.dataSource.username=root
db.dataSource.password=root
Sample "production.properties"
示例“production.properties”
db.logsDataSource.url=jdbc:mariadb://111.111.111.111:3306/logs
db.logsDataSource.username=admin
db.logsDataSource.password=xyzqer
db.dataSource.url=jdbc:mysql://111.111.111.111:3306/carsinfo
db.dataSource.username=admin
db.dataSource.password=safasf@mn
For using these properties file, you can make use of REsource as mentioned below
要使用这些属性文件,您可以使用下面提到的资源
PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("classpath:"+System.getenv("SERVER_TYPE")+"DB.properties");
configurer.setLocation(resource);
configurer.postProcessBeanFactory(beanFactory);
SERVER_TYPE can be defined as the environment variable with appropriate values for local and production environment.
SERVER_TYPE 可以定义为具有适用于本地和生产环境的适当值的环境变量。
With these changes the appplicationContext.xml will have the following changes
通过这些更改 appplicationContext.xml 将有以下更改
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${db.dataSource.url}" />
<property name="username" value="${db.dataSource.username}" />
<property name="password" value="${db.dataSource.password}" />
Hope this helps .
希望这可以帮助 。
回答by Justin Patel
Declare the property place holder as follows
声明属性占位符如下
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="locations">
<list>
<value>file:///path.to.your.app.config.properties</value>
</list>
</property>
</bean>
Then lets say you want to read System.property("java.io.tmpdir")
for your Tomcat bean or any bean then add following in your properties file:
然后假设您要读取System.property("java.io.tmpdir")
Tomcat bean 或任何 bean,然后在属性文件中添加以下内容:
tomcat.tmp.dir=${java.io.tmpdir}