
Originally Posted by
thuss
We have one database per state (e.g. CA, NY, FL) and our DAO layer may need to get a JdbcTemplate for any of the 50 state databases.
What's the most efficient way in Spring to configure the 50 jdbc templates? Do I have to enumerate each one?
And is there some sort of JdbcTemplateFactory I can dependency inject in my DAO's and then in the DAO say factory.getJdbcTemplate("FL");
Maybe this will work for you:
Code:
<bean name="CA_dataSource" class="...">
properties ...
</bean>
<bean name="dataSourceMap" class="java.util.HashMap">
<constructor-arg>
<map>
<entry key="CA"><ref bean="CA_dataSource"/></entry>
</map>
</constructor-arg>
</bean>
<bean name="myDao" class="...">
<property name="dataSourceMap">
<ref bean="dataSourceMap"/>
</property>
</bean>
In your DAO you could then do this:
Code:
private Map dataSourceMap = null;
public void setDataSourceMap(Map dataSourceMap) { this.dataSourceMap = dataSourceMap; }
private JdbcTemplate getJdbcTemplate(String state) {
return new JdbcTemplate((DataSource)this.dataSourceMap.get(state));
}