Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在SpringXML配置中注入环境变量?_Java_Spring_Amazon Web Services_Environment Variables - Fatal编程技术网

Java 如何在SpringXML配置中注入环境变量?

Java 如何在SpringXML配置中注入环境变量?,java,spring,amazon-web-services,environment-variables,Java,Spring,Amazon Web Services,Environment Variables,在我们设置环境变量之后,AWS在中讨论了System.getProperty(“JDBC\u CONNECTION\u STRING”)。一切都很好,除了我不能在SpringXML配置代码中调用System.getProperty,也不能调用资源包快捷方式,因为资源包本身必须以某种方式提取这些环境变量来为它们服务。您能帮我将这个示例配置转换为使用环境变量吗?:-) 300000 60000 我无法理解这里的人们做什么: 哪一个适用于最新的spring版本?首先在配置中添加一个元素 然后只

在我们设置环境变量之后,AWS在中讨论了
System.getProperty(“JDBC\u CONNECTION\u STRING”)
。一切都很好,除了我不能在SpringXML配置代码中调用
System.getProperty
,也不能调用资源包快捷方式,因为资源包本身必须以某种方式提取这些环境变量来为它们服务。您能帮我将这个示例配置转换为使用环境变量吗?:-)


300000
60000
我无法理解这里的人们做什么:

哪一个适用于最新的spring版本?

首先在配置中添加一个
元素


然后只需在配置中使用占位符



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

如果使用类加载属性文件,则可以将属性
systemPropertiesMode
设置为值
SYSTEM\u PROPERTIES\u MODE\u 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>

classpath://file.properties                  
Spring将以以下方式加载系统属性:

在尝试指定的属性之前,请先检查系统属性。 这允许系统属性覆盖任何其他属性源


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

对于使用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}