title: Spring Multiple PropertyPlaceholderConfigurer files and database
body: <p>I want to load the properties from my java application used the Spring Beans, through the database, however I need to load the properties of the connection to the database from a file.</p>
  
  <p>So what I want is the following:</p>
  
  <ol>
  <li>Load the file db.Properties </li>
  <li>Initialize the beans of DataSources</li>
  <li>Load the remaining properties Base</li>
  </ol>
  
  <p>At the end, have access to the loaded properties from both sources.</p>
  
  <p>I tried the following way.</p>
  
  <p><strong>application-context.xml:</strong></p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
  
  &lt;!-- ************************************* --&gt;
  &lt;!--        1:  Files Properties Load      --&gt;
  &lt;!-- ************************************* --&gt;
  &lt;bean id="placeholderPropertiesFile" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
      &lt;property name="locations"&gt;
          &lt;list&gt;
              &lt;value&gt;file:./properties/dataBase.properties&lt;/value&gt;
              &lt;value&gt;file:./properties/webServicesUserPass.properties&lt;/value&gt;
          &lt;/list&gt;
      &lt;/property&gt;
      &lt;property name="placeholderPrefix" value="$file{" /&gt;
      &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /&gt;
      &lt;property name="ignoreUnresolvablePlaceholders" value="true" /&gt;
      &lt;property name="order" value="0" /&gt;
  &lt;/bean&gt;
  
  
  
  &lt;!-- ************************************* --&gt;
  &lt;!--        2:  Data Sources Load          --&gt;
  &lt;!-- ************************************* --&gt;  
  
  &lt;bean id="dataSourceOracleRead" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="$file{db.driverClassName}" /&gt;
      &lt;property name="url" value="$file{dbRead.url}" /&gt;
      &lt;property name="username" value="$file{dbRead.username}" /&gt;
      &lt;property name="password" value="$file{dbRead.password}" /&gt;
      &lt;property name="poolPreparedStatements" value="true" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dataSourceOracleWrite" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="$file{db.driverClassName}" /&gt;
      &lt;property name="url" value="$file{dbWrite.url}" /&gt;
      &lt;property name="username" value="$file{dbWrite.username}" /&gt;
      &lt;property name="password" value="$file{dbWrite.password}" /&gt;
      &lt;property name="poolPreparedStatements" value="true" /&gt;
  &lt;/bean&gt;
  
  
  &lt;!-- ************************************* --&gt;
  &lt;!--        3:  Database Properties Load   --&gt;
  &lt;!-- ************************************* --&gt;
  &lt;bean id="placeholderPropertiesDatabase" class="xxx.xxx.DbPropertyPlaceholderConfigurer" &gt;
      &lt;property name="dt" ref="dataSourceOracleRead" /&gt;
      &lt;property name="table" value="CONFIG0" /&gt;
      &lt;property name="key" value="I_CODIGO" /&gt;
      &lt;property name="value" value="C_VALOR" /&gt;
  
      &lt;property name="placeholderPrefix" value="$db{" /&gt;
      &lt;property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /&gt;
      &lt;property name="ignoreUnresolvablePlaceholders" value="false" /&gt;
      &lt;property name="order" value="1" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p><strong>DbPropertyPlaceholderConfigurer:</strong></p>
  
  <pre><code>public class DbPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
      private DataSource dt;
  private String key;
  private String value;
  private String table;
  
  @Override
  protected void loadProperties(final Properties props) throws IOException {
      if (null == props) {
          throw new IOException("No properties passed by Spring framework - cannot proceed.");
      }
  
      String sql = String.format("SELECT %s, %s FROM %s", key, value, table);
  
      try {
          JdbcTemplate t = new JdbcTemplate(dt);
          t.query(sql, new RowCallbackHandler() {
              @Override
              public void processRow(ResultSet rs) throws SQLException {
                  String auxKey = rs.getString(key);
                  String auxValue = rs.getString(value);
  
                  if (null == auxKey || null == auxValue) {
                      throw new SQLException("Configuration database contains empty data. Name='" + (auxKey == null ? "" : auxKey)
                              + "', Value='" + (auxValue == null ? "" : auxValue) + "'.");
                  }
  
                  props.setProperty(auxKey, auxValue);
              }
          });
      } catch (Exception e) {
          logger.fatal("There is an error in either 'application.properties' or the configuration database.");
          throw new IOException(e);
      }
  
      if (props.size() == 0) {
          logger.fatal("The configuration database could not be reached or does not contain any properties in '" + table
                  + "'.");
      } else {
          logger.info("Application config info loaded from configuration database.");
      }
  }
  
  public void setDt(DataSource dt) {
      this.dt = dt;
  }
  
  public void setKey(String key) {
  this.key = key;
  }
  
  public void setValue(String value) {
  this.value = value;
  }
  
  public void setTable(String table) {
  this.table = table;
  }
  }
  </code></pre>
  
  <p><strong>Error:</strong></p>
  
  <pre><code>Exception in thread "Main Thread" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.IOException: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '$file{db.driverClassName}'
      at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:87)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:661)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:139)
      at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(ClassPathXmlApplicationContext.java:84)
      at xxx.xxx.xxx.BatchMain.main(BatchMain.java:35)
  Caused by: java.io.IOException: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '$file{db.driverClassName}'
      at xxx.xxx.xxx.xxx.propertiesLoader.DbPropertyPlaceholderConfigurer.loadProperties(DbPropertyPlaceholderConfigurer.java:60)
      at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
      at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
      at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:664)
      ... 4 more
  </code></pre>
  
  <p>If I choose to initialize the bean "dataSourceOracleRead" in application-context.xml instead of getting trough the file, access the database works fine.</p>
  
  <pre><code>&lt;bean id="dataSourceOracleRead" class="org.apache.commons.dbcp.BasicDataSource"&gt;
  &lt;property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /&gt;
  &lt;property name="url" value="jdbc:oracle:thin:@10.10.10.10:1521:bdtest" /&gt;
  &lt;property name="username" value="test" /&gt;
  &lt;property name="password" value="qwerty" /&gt;
  &lt;property name="poolPreparedStatements" value="true" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>I think when I set the second "PropertyPlaceholderConfigurer" I automatically lose the properties already charged through the first (properties file).</p>
  
  <p>If I have two "PropertyPlaceholderConfigurer" in which one load one of the files and second loads the second file I do not lose the charged properties through the 1st "PropertyPlaceholderConfigurer".</p>
  
  <p>Ps: I'm using version 3.1.2 of spring   </p>