Java 如何在spring xml配置中注入环境变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18744663/
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 inject environmental variables inside spring xml configuration?
提问by Aubergine
AWS talks about System.getProperty("JDBC_CONNECTION_STRING")in http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.managing.htmlafter we set up our environmental variables. All great except I can't call System.getPropertyinside my Spring XML configuration code nor I can call for resource bundle shortcuts since resources bundle itself has to extract somehow these environmental variables to serve them. Could you kindly help me please to convert this example config to use environmental variables ? :-)
在我们设置环境变量后,AWSSystem.getProperty("JDBC_CONNECTION_STRING")在http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.managing.html 中谈到 。一切都很好,除了我不能System.getProperty在我的 Spring XML 配置代码中调用,也不能调用资源包快捷方式,因为资源包本身必须以某种方式提取这些环境变量来为它们提供服务。你能帮我转换这个示例配置以使用环境变量吗?:-)
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://secrethost:007/whois?autoReconnect=true" />
<property name="username" value="bond" />
<property name="password" value="abuginsidemistycorner" />
<property name="initialSize" value="100" />
<property name="minEvictableIdleTimeMillis">
<value>300000</value>
</property>
<property name="timeBetweenEvictionRunsMillis">
<value>60000</value>
</property>
<property name="maxIdle" value="20" />
</bean>
I was not able to understand what do people do here:
我无法理解人们在这里做什么:
Can I use an Environment variable based location for Spring FileSystemResource?which would work for recent spring version?
我可以为 Spring FileSystemResource 使用基于环境变量的位置吗?哪个适用于最近的春季版本?
采纳答案by M. Deinum
First add a <context:property-placeholder .. />element to your configuration.
首先<context:property-placeholder .. />在您的配置中添加一个元素。
<context:property-placeholder />
Then simply use placeholders in your config.
然后只需在您的配置中使用占位符。
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${JDBC_CONNECTION_STRING}" />
<property name="username" value="bond" />
<property name="password" value="abuginsidemistycorner" />
<property name="initialSize" value="100" />
<property name="minEvictableIdleTimeMillis" value="30000" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="maxIdle" value="20" />
</bean>
Make sure that the placeholder names match your variables you have setup.
确保占位符名称与您设置的变量匹配。
回答by MPavesi
If you are using the class org.springframework.beans.factory.config.PropertyPlaceholderConfigurerto load the property files, you can set the property systemPropertiesModeto the value SYSTEM_PROPERTIES_MODE_OVERRIDE.
如果您使用类org.springframework.beans.factory.config.PropertyPlaceholderConfigurer来加载属性文件,则可以将属性systemPropertiesMode设置为值SYSTEM_PROPERTIES_MODE_OVERRIDE。
In the spring.xml you'll have this bean:
在 spring.xml 中,您将拥有这个 bean:
<bean id="propertyPlaceholder"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="locations">
<list>
<value>classpath://file.properties</value>
</list>
</property>
</bean>
Spring will load the system properties in this way:
Spring 将以这种方式加载系统属性:
Check system properties first, before trying the specified properties. This allows system properties to override any other property source.
在尝试指定的属性之前,首先检查系统属性。这允许系统属性覆盖任何其他属性源。
In this way you should be able to read the system properties as normal properties.
通过这种方式,您应该能够将系统属性作为正常属性读取。
回答by Tomasz Janisiewicz
For someone who use JavaConfig:
对于使用 JavaConfig 的人:
In @Configuration file we need to have:
在@Configuration 文件中,我们需要:
@Bean
public static PropertyPlaceholderConfigurer properties() {
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ClassPathResource[] resources = new ClassPathResource[ ] {
new ClassPathResource("db.properties")
};
ppc.setLocations( resources );
ppc.setIgnoreUnresolvablePlaceholders( true );
ppc.setSearchSystemEnvironment(true);
return ppc;
}
@Value("${db.url}")
private String dbUrl;
@Value("${db.driver}")
private String dbDriver;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPassword;
@Bean
public DataSource db() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setUrl(dbUrl);
dataSource.setDriverClassName(dbDriver);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
return dataSource;
}
important is line: ppc.setSearchSystemEnvironment(true);
重要的是行: ppc.setSearchSystemEnvironment(true);
after that in db.properties, I have:
在 db.properties 之后,我有:
db.url = ${PG_URL}
db.driver = ${PG_DRIVER}
db.username = ${PG_USERNAME}
db.password = ${PG_PASSWORD}

