spring实现多数据源,主需要向spring工厂动态注册数据源即可。代码:
package com.htxx.service.dao;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
public class DynamicDataSource implements ApplicationContextAware {
private static ApplicationContext app = new ClassPathXmlApplicationContext(
"/applicationContext.xml");
public void setApplicationContext(ApplicationContext app)
throws BeansException {
this.app = app;
}
private void regDynamicBean() throws Exception {
// 解析属性文件,得到数据源Map
Map<String, DataSourceInfo> mapCustom = parsePropertiesFile("hummer.properties");
// 把数据源bean注册到容器中
addSourceBeanToApp(mapCustom);
addJdbcTemplateToApp(mapCustom);
System.out.println(app.getBean("mydata" + 1));
}
/**
* 功能说明:根据DataSource创建bean并注册到容器中
*
* @param acf
* @param mapCustom
*/
private void addSourceBeanToApp(Map<String, DataSourceInfo> mapCustom)
throws Exception {
DefaultListableBeanFactory acf = (DefaultListableBeanFactory) app
.getAutowireCapableBeanFactory();
BeanDefinition beanDefinition;
Iterator<String> iter = mapCustom.keySet().iterator();
while (iter.hasNext()) {
String beanKey = iter.next();
// 得到Bean定义,并添加到容器中
beanDefinition = new ChildBeanDefinition("dataSource");
// 注意:必须先注册到容器中,再得到Bean进行修改,否则数据源属性不能有效修改
acf.registerBeanDefinition(beanKey, beanDefinition);
// 再得到数据源Bean定义,并修改连接相关的属性
BasicDataSource cpds = (BasicDataSource) app.getBean(beanKey);
cpds.setUrl(mapCustom.get(beanKey).connUrl);
cpds.setUsername(mapCustom.get(beanKey).userName);
cpds.setPassword(mapCustom.get(beanKey).password);
cpds.setDriverClassName("oracle.jdbc.driver.OracleDriver");
}
}
/**
* 功能说明:根据DataSource创建bean并注册到容器中
*
* @param acf
* @param mapCustom
*/
private static void addJdbcTemplateToApp(
Map<String, DataSourceInfo> mapCustom) throws Exception {
DefaultListableBeanFactory acf = (DefaultListableBeanFactory) app
.getAutowireCapableBeanFactory();
BeanDefinition beanDefinition;
Iterator<String> iter = mapCustom.keySet().iterator();
while (iter.hasNext()) {
String beanKey = iter.next();
// 得到Bean定义,并添加到容器中
beanDefinition = new ChildBeanDefinition("jdbcTemplate");
// 注意:必须先注册到容器中,再得到Bean进行修改,否则数据源属性不能有效修改
acf
.registerBeanDefinition("jdbcTemplate" + beanKey,
beanDefinition);
// 再得到数据源Bean定义,并修改连接相关的属性
JdbcTemplate cpds = (JdbcTemplate) app.getBean("jdbcTemplate"
+ beanKey);
;
cpds.setDataSource(app.getBean(beanKey, DataSource.class));
}
}
/**
* 功能说明:解析属性文件,得到数据源Map
*
* @return
* @throws IOException
*/
private Map<String, DataSourceInfo> parsePropertiesFile(String fileName)
throws IOException {
// 属性文件
Map<String, DataSourceInfo> mapDataSource = new HashMap<String, DataSourceInfo>();
DataSourceInfo dataSourceInfo = new DataSourceInfo();
dataSourceInfo.connUrl = "##";
dataSourceInfo.password = "##";
dataSourceInfo.userName = "##";
for (int i = 0; i < 10; i++) {
mapDataSource.put("mydata" + i, dataSourceInfo);
}
return mapDataSource;
}
public static void main(String[] args) throws Exception {
new DynamicDataSource().regDynamicBean();
for (int i = 0; i < 10; i++) {
JdbcTemplate jdbcTemplate = app.getBean("jdbcTemplate" + "mydata"
+ i, JdbcTemplate.class);
// JdbcTemplate jdbcTemplate =
// app.getBean("jdbcTemplate",JdbcTemplate.class);
System.out.println("jdbcTemplate id =" + jdbcTemplate);
System.out.println(jdbcTemplate.getDataSource().getConnection());
jdbcTemplate.execute("insert into ddd(table_name) values('test" + i
+ "')");
}
}
private class DataSourceInfo {
public String connUrl;
public String userName;
public String password;
}
}