Dynamically initialize multiple data sources in Spring
<p>In my spring application , I need to dynamically initialize multiple data sources based on some values set up in the application configuration.
I am aware of the AbstractRoutingDataSource class provided by spring jdbc library but it helps only when you need to initialize a single data source based on a single look up key value at a time.</p>

<p>Is it possible to extend the AbstractRoutingDataSource and change its behavior to support multiple key look up and data source resolution? Is there any other alternative approach ? <a href="https://spring.io/blog/2007/01/23/dynamic-datasource-routing/" rel="nofollow">Reference</a></p>

<p>Basically I am trying to achieve something like this through AbstractDataSourceRouter class:</p>

<pre><code>public class DataSourceRouter extends AbstractRoutingDataSource {


@Value("${com.listdb.datasource.switch}")
private short listDBSwitch;

@Value("${com.scoringdb.datasource.switch}")
private short scoringDbSwitch;

@Value("${com.configmaster.datasource.switch}")
private short configDbSwitch;


private List&lt;String&gt; configuredDataSources;
/**
 * Determine the current lookup key. This will typically be
 * implemented to check a thread-bound transaction context.
 * &lt;p&gt;Allows for arbitrary keys. The returned key needs
 * to match the stored lookup key type, as resolved by the
 * {@link #resolveSpecifiedLookupKey} method.
 */
@Override
protected Object determineCurrentLookupKey() {

    if(ListUtil.isListNotEmpty(configuredDataSources)) {

        configuredDataSources =new ArrayList&lt;String&gt;();


        String listDBString = (listDBSwitch == 1)?DataSources.LIST.toString() : null;
        String configDBString = (configDbSwitch == 1) ? DataSources.CONFIGMASTER.toString() :null;
        String scoringDBString = (scoringDbSwitch == 1) ? DataSources.SCORING.toString() : null;

        /**
         * Add all configured data source keys for look up
         */
        configuredDataSources.add(listDBString);
        configuredDataSources.add(configDBString);
        configuredDataSources.add(scoringDBString);

    }

    return configuredDataSources;
}
</code></pre>

<p>}</p>

<p>Any help/suggestions?</p>