如何在Spring XML配置文件中注入环境变量?

34
AWS在我们设置环境变量之后,谈论了System.getProperty("JDBC_CONNECTION_STRING"),参见http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.managing.html。所有都很好,但我不能在我的Spring XML配置代码中调用System.getProperty,也不能调用资源包快捷方式,因为资源包本身必须以某种方式提取这些环境变量才能提供服务。你能帮我把这个示例配置转换为使用环境变量吗?:-)
<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>

我不明白人们在这里做什么:

我能否在Spring FileSystemResource中使用基于环境变量的位置? 这是否适用于最近版本的Spring?

3个回答

51
首先将<context:property-placeholder .. />元素添加到您的配置中。
<context:property-placeholder />

然后在您的配置文件中使用占位符即可。

<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>

确保占位符名称与您设置的变量名称匹配。


9
如果您正在使用类org.springframework.beans.factory.config.PropertyPlaceholderConfigurer来加载属性文件,则可以将属性systemPropertiesMode设置为值SYSTEM_PROPERTIES_MODE_OVERRIDE
在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会按照以下方式加载系统属性:

先检查系统属性,然后尝试指定的属性。这使得系统属性可以覆盖任何其他属性来源。

以这种方式,您应该能够将系统属性读取为普通属性。


5

对于使用JavaConfig的人:

在@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;
}

重要的是这一行代码:ppc.setSearchSystemEnvironment(true);

在此之后,在db.properties文件中,我有以下内容:

db.url = ${PG_URL}
db.driver = ${PG_DRIVER}
db.username = ${PG_USERNAME}
db.password = ${PG_PASSWORD}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接