How to inject environmental variables inside spring xml configuration?

后端 未结 3 1052
借酒劲吻你
借酒劲吻你 2021-01-30 21:50

AWS talks about System.getProperty(\"JDBC_CONNECTION_STRING\") in http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.managing.html after we s

相关标签:
3条回答
  • 2021-01-30 22:31

    First add a <context:property-placeholder .. /> element to your configuration.

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

    0 讨论(0)
  • 2021-01-30 22:38

    For someone who use JavaConfig:

    In @Configuration file we need to have:

    @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);

    after that in db.properties, I have:

    db.url = ${PG_URL}
    db.driver = ${PG_DRIVER}
    db.username = ${PG_USERNAME}
    db.password = ${PG_PASSWORD}
    
    0 讨论(0)
  • 2021-01-30 22:49

    If you are using the class org.springframework.beans.factory.config.PropertyPlaceholderConfigurer to load the property files, you can set the property systemPropertiesMode to the value SYSTEM_PROPERTIES_MODE_OVERRIDE.

    In the spring.xml you'll have this 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:

    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.

    0 讨论(0)
提交回复
热议问题