spring 动态注册数据源

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;
	}
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值