Spring:访问applicationConfig.xml中的环境变量

时间:2015-11-16 11:17:13

标签: spring

我想将我的数据库凭据和其他机密值设置为环境变量的一部分。有没有办法访问applicationConfig.xml

中的环境变量

我试过了<property name="username" value="#{systemEnvironment['db_username']}" />。但是这没用。我错过了什么吗?

许多人告诉我如何从属性文件中访问值。我需要直接访问环境变量。

我的代码如下: -

<context:component-scan base-package="org.dhana.*" />

    <context:annotation-config />

    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>



    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url"
            value="xxxx" />
        <property name="username" value="${db_username}" />
        <property name="password" value="xxxxx" />
    </bean>

    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>

6 个答案:

答案 0 :(得分:9)

您可能需要设置 searchSystemEnvironment 值才能使其正常工作。

<bean id="propertyPlaceholderConfigurer"   
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchSystemEnvironment" value="true" />
</bean>

然后,我们应该可以访问$ {MY_ENV_VAR}。

答案 1 :(得分:1)

您可以使用$ {}来使用环境变量 例如,如果要添加一个配置文件,该文件的位置取决于名为“env_config_path”的环境变量,您可以使用:

  <context:property-placeholder location="file:${env_config_path}/configuration.properties" />

我不建议您将环境变量用于具体值,我认为使用环境变量指向的文件是更好的方法,如我的例子所示。

使用如下的paremeter加载java程序时,可以强制使用环境变量值:

-Denv_resource_path="/Users/mylogin/work/myproject/development_environment_resources" 

答案 2 :(得分:0)

您更愿意使用&#34; systemProperties&#34;,就像这样 -

value="#{systemProperties['db_username']}"

答案 3 :(得分:0)

  1. 创建一个属性文件(xxxx.properties)并将所有与环境相关的信息放在属性文件中并将其放在类路径中。
  2. 对于ex:将您的环境值设置为如下所示

        jdbc.userName = xxx
        jdbc.password = yyy
    
    1. 在您的xml文件中。添加以下代码

      <bean id = "prop"
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
          <value>classpath*:xxxx.properties</value>
      </property>
      

    2. 现在您可以从您的属性文件中调用环境变量,如下所示

      <bean id="dataSource" 
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${}"/>
        <property name="url" value="${}"/>
        <property name="username" value="${jdbc.userName}"/>
        <property name="password" value="${jdbc.password}"/>      </bean>  
      
    3. 只需在值

      中指定属性键字段即可

答案 4 :(得分:0)

添加

<context:property-placeholder />

XML文件开头附近。

此外,如果您使用的是Eclipse,则需要重新启动它才能获取帐户级环境变量的所有更改。

答案 5 :(得分:-2)

您可以使用前缀值$ {_ var:MY_ENV_OR_PROPERTY}

扩展PropertyPlaceholderConfigurer类并在属性值中使用env或properties变量

可选:使用-Denv = -dev创建其他配置,例如:jdbc-dev.properties。

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://${_var:MARIADB_SERVICE_HOST}:${_var:MARIADB_SERVICE_PORT}/mydb?serverTimezone=UTC&useSSL=false&autoReconnect=true
jdbc.username=root

的applicationContext.xml

<bean id="jdbc-properties" class="com.naskar.spring.VariablePropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc#{systemProperties['env']}.properties"/>
    <property name="searchSystemEnvironment" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

VariablePropertyPlaceholderConfigurer.java

class VariablePropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private static String resolveValueWithEnvVars(String value) {
        if (null == value) {
            return null;
        }

        Pattern p = Pattern.compile('\\$\\{_var:(\\w+)\\}|\\$(\\w+)');
        Matcher m = p.matcher(value);
        StringBuffer sb = new StringBuffer();

        while (m.find()) {
            String envVarName = null == m.group(1) ? m.group(2) : m.group(1);
            String envVarValue = System.getProperty(envVarName);
            if(envVarValue == null) {
                envVarValue = System.getenv(envVarName);
            }
            m.appendReplacement(sb, null == envVarValue ? "" : Matcher.quoteReplacement(envVarValue));
        }

        m.appendTail(sb);

        return sb.toString();
    }

    @Override
    protected String convertPropertyValue(String originalValue) {
        return resolveValueWithEnvVars(originalValue);
    }

}