我是春天的新手。
我有多个数据库,但任何给定时间都有一个数据库打开,具体取决于用户选择的数据库。所以我有一个定义如下的bean。所以我的问题是如何使用下面给定的bean来实例化数据源用户选择并在我的类中使用它。任何想法都将不胜感激。
<bean id="dataSources" class="java.util.TreeMap">
<constructor-arg>
<map>
<entry key="dp1www" value-ref="dataSourceDp1www" />
<entry key="dp2www" value-ref="dataSourceDp2www" />
<entry key="sp0www" value-ref="dataSourceSp0www" />
<entry key="sp1www" value-ref="dataSourceSp1www" />
<entry key="sp2www" value-ref="dataSourceSp2www" />
</map>
</constructor-arg>
</bean>
<bean id="dataSourceDp1www" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:@sf1-hab-ppb-ww.home.com:1525/dp1www1.home.com" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="select" />
<property name="password" value="######" />
</bean>
<bean id="dataSourceDp2www" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:@sf1-hbl-ppb-ww.home.com:1525/dp2www1.home.com" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="select" />
<property name="password" value="######" />
</bean>
<bean id="dataSourceSp0www" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:@sf1-hbl-ppb-ww.home.com:1525/sp0www1.home.com" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="select" />
<property name="password" value="######" />
</bean>
<bean id="dataSourceSp1www" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:oracle:thin:@sf1-hab-ppb-ww.home.com:1525/sp1www1.home.com" />
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="username" value="select" />
<property name="password" value="######" />
</bean>
答案 0 :(得分:1)
您可以使用@Autowired
注释来注入TreeMap
实例:
@Autowired
private TreeMap<String, DataSource> dataSources;
....
DataSource dataSource = dataSources.get("dp1www");
或者您可以直接注入特定数据源,甚至无需创建TreeMap
@Autowired
private DataSource dataSourceDp1www;