title: Dynamically create spring beans and alter properties on existing beans
body: <p>I sucessfully managed to implement dynamic changing of database connections by following <a href="http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/" rel="nofollow">http://blog.springsource.com/2007/01/23/dynamic-datasource-routing/</a> article.</p>
  
  <p>But now the problem is, I have a list of database urls in a configuration file that is managed by a legacy application.</p>
  
  <p>Is there a way to create beans in that Spring context from a list of values (i.e. Year2011DataSource, Year2012DataSource,...) and populate map of the dataSource bean with those beans that were just created?</p>
  
  <pre><code>&lt;!-- Property file located in the legacy application's folder --&gt;
  &lt;context:property-placeholder location="file:///D:/config.properties" /&gt;
  
  &lt;!-- Shared data source properties are read from the config.properties file --&gt;
  &lt;bean id="parentDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" abstract="true"&gt;
      &lt;property name="driverClassName" value="${db.driver}" /&gt;
      &lt;property name="username" value="${db.user}" /&gt;
      &lt;property name="password" value="${db.password}" /&gt;
  &lt;/bean&gt;
  
  &lt;!-- Database urls by year --&gt;
  &lt;bean id="Year2012DataSource" parent="parentDataSource"&gt;
      &lt;property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2012" /&gt;
  &lt;/bean&gt;
  &lt;bean id="Year2011DataSource" parent="parentDataSource"&gt;
      &lt;property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2011" /&gt;
  &lt;/bean&gt;
  &lt;bean id="Year2010DataSource" parent="parentDataSource"&gt;
      &lt;property name="url" value="jdbc:sqlserver://localhost;databaseName=DbName_v570_2010" /&gt;
  &lt;/bean&gt;
  &lt;!-- ... and so on, these should instead be populated dynamically ... --&gt;
  
  &lt;!-- DbConnectionRoutingDataSource extends AbstractRoutingDataSource --&gt;
  &lt;bean id="dataSource" class="someProject.DbConnectionRoutingDataSource"&gt;
      &lt;property name="targetDataSources"&gt;
          &lt;map key-type="int"&gt;
              &lt;entry key="2011" value-ref="Year2011DataSource" /&gt;
              &lt;entry key="2010" value-ref="Year2010DataSource" /&gt;
              &lt;!-- ... and so on, these also should instead be populated dynamically ... --&gt;
          &lt;/map&gt;
      &lt;/property&gt;
      &lt;property name="defaultTargetDataSource" ref="Year2012DataSource" /&gt;
  &lt;/bean&gt;
  </code></pre>