title: How to use Spring AbstractRoutingDataSource with dynamic datasources?
body: <p>I am working in a project using Spring, Spring Data JPA, Spring Security, Primefaces...</p>
  
  <p>I was following <a href="http://spring.io/blog/2007/01/23/dynamic-datasource-routing/" rel="noreferrer">this tutorial</a> about dynamic datasource routing with spring.</p>
  
  <p>In this tutorial, you can only achieve dynamic datasource switching between a pre-defined datasources.</p>
  
  <p>Here is a snippet of my code :</p>
  
  <p><strong>springContext-jpa.xml</strong></p>
  
  <pre><code>&lt;bean id="dsCgWeb1" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      &lt;property name="driverClassName" value="${jdbc.driverClassName.Cargest_web}"&gt;&lt;/property&gt;
      &lt;property name="url" value="${jdbc.url.Cargest_web}"&gt;&lt;/property&gt;
      &lt;property name="username" value="${jdbc.username.Cargest_web}"&gt;&lt;/property&gt;
      &lt;property name="password" value="${jdbc.password.Cargest_web}"&gt;&lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="dsCgWeb2" class="org.apache.commons.dbcp.BasicDataSource"&gt;
      // same properties, different values ..
  &lt;/bean&gt;
  
  &lt;!--  Generic Datasource [Default : dsCargestWeb1]  --&gt;
  &lt;bean id="dsCgWeb" class="com.cargest.custom.CargestRoutingDataSource"&gt;
      &lt;property name="targetDataSources"&gt;
          &lt;map&gt;
              &lt;entry key="1" value-ref="dsCgWeb1" /&gt;
              &lt;entry key="2" value-ref="dsCgWeb2" /&gt;
          &lt;/map&gt;
      &lt;/property&gt;
      &lt;property name="defaultTargetDataSource" ref="dsCgWeb1" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>What i want to do is to make the <strong>targetDataSources</strong> map dynamic same as its elements too. </p>
  
  <p>In other words, i want to fetch a certain database table, use properties stored in that table to create my datasources then put them in a map like <strong>targetDataSources</strong>.</p>
  
  <p>Is there a way to do this ?</p>