Spring: accessing environment variables inside applicationConfig.xml

I would like to set my db credentials and other secret values ​​as part of environment variables. Is there a way to access environment variables inside applicationConfig.xml

I have tried <property name="username" value="#{systemEnvironment['db_username']}" />. However, this did not work. Did I miss something?

Many have told me how to access values ​​from a properties file. I need to access the environment variable directly.

My code is as follows: -

<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>
+4
source share
6 answers

You may need to set searchSystemEnvironment to make it work.

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

Then we must have access to $ {MY_ENV_VAR}.

+9
source

, ${} , , 'env_config_path', :

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

, , , , .

Java-, , :

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

"systemProperties", :

value="#{systemProperties['db_username']}"
0
  • (xxxx.properties) .

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>  
    

0

<context:property-placeholder />

XML.

, Eclipse, , - .

0

PropertyPlaceholderConfigurer env properties , ${_ var: MY_ENV_OR_PROPERTY}

: -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);
    }

}
-2

Source: https://habr.com/ru/post/1616043/


All Articles