Spring(AbstractRoutingDataSource)实现动态数据源切换
2016-06-26 21:10
567 查看
一 概述
今天在看新工程代码的时候,发现数据源是多个,用了spring的AbstractRoutingDataSource实现动态数据源切换。就是在程序运行时,把数据源动态织入到程序中,从而选择读取主库还是从库。主要使用的技术是:annotation,Spring AOP ,反射。(注意这里的多数据源不牵扯到跨库的事物)。在网上找了篇类似的的实现,转载过来。
—————————————以下为转载—————————————————————
原文地址:http://linhongyu.blog.51cto.com/6373370/1615895
原文如下,为了便于阅读,有所改动:
扩展AbstractRoutingDataSource类,并重写其中的determineCurrentLookupKey()方法,来实现数据源的切换:
package com.datasource.test.util.database;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 获取数据源(依赖于spring)
* @author linhy
*/
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DataSourceHolder.getDataSource();
}
}DataSourceHolder这个类则是我们自己封装的对数据源进行操作的类:
package com.datasource.test.util.database;
/**
* 数据源操作
* @author linhy
*/
public class DataSourceHolder {
//线程本地环境
private static final ThreadLocal<String> dataSources = new ThreadLocal<String>();
//设置数据源
public static void setDataSource(String customerType) {
dataSources.set(customerType);
}
//获取数据源
public static String getDataSource() {
return (String) dataSources.get();
}
//清除数据源
public static void clearDataSource() {
dataSources.remove();
}
}
@Named("userService")
public class UserService
{
@Inject
private UserDao userDao;
@DataSource("master")
@Transactional(propagation=Propagation.REQUIRED)
public void updatePasswd(int userid,String passwd)
{
User user = new User();
user.setUserid(userid);
user.setPassword(passwd);
userDao.updatePassword(user);
}
@DataSource("slave")
@Transactional(propagation=Propagation.REQUIRED)
public User getUser(int userid)
{
User user = userDao.getUserById(userid);
System.out.println("username------:"+user.getUsername());
return user;
}
}二、 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 数据库相关配置 放在这里 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> bean id="dataSource" class="com.westone.datasource.DbRouteDataSource">
<property name="targetDataSources">
<map>
<!-- write -->
<entry key="master" value-ref="master"></entry>
<!-- read -->
<entry key="slave" value-ref="slave"></entry>
</map>
</property>
</bean>
<bean id="master" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverclass}" />
<property name="url" value="${jdbc.masterurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<bean id="slave" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverclass}" />
<property name="url" value="${jdbc.slaveurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis/mybatis.cfg.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置mapper的映射扫描器 根据包中定义的接口自动生成dao的实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.westone.dao"></property>
</bean>
<!-- 为业务逻辑层的方法解析@DataSource注解 为当前线程的routeholder注入数据源key -->
<bean id="aspectBean" class="com.westone.datasource.aspect.DataSourceAspect"></bean>
<aop:config>
<aop:aspect id="dataSourceAspect" ref="aspectBean">
<aop:pointcut id="dataSourcePoint" expression="execution(public * com.westone.service.*.*(..))" />
<aop:before method="beforeDaoMethod" pointcut-ref="dataSourcePoint"/>
</aop:aspect>
</aop:config>
<!-- 事务管理器配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解驱动 在业务逻辑层上使用@Transactional 注解 为业务逻辑层管理事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
最后测试业务逻辑层的方法发现 可以根据方法上的@DataSource("master") 注解配置不同的数据源key 使用动态数据源。 不需要再业务逻辑方法层中定义不同的数据源对象,人为的使用不同的数据源操作数据。
*********************原文结束********************
补充下背景知识:
切面(Aspect):官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
连接点(Joinpoint) :程序执行过程中的某一行为,例如,UserService.get的调用或者UserService.delete抛出异常等行为。
通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如ServiceAspect。
切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定。
目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
AOP代理(AOP Proxy) :在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config>的 proxy-target-class属性设为true。
通知(Advice)类型:
前置通知(Before advice):在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法。
后置通知(After advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,ServiceAspect中的returnAfter方法,所以Teser中调用UserService.delete抛出异常时,returnAfter方法仍然执行。
返回后通知(After return advice):在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
环绕通知(Around advice):包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,ServiceAspect中的around方法。
抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知。ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,ServiceAspect中的returnThrow方法。
注:可以将多个通知应用到一个目标对象上,即可以将多个切面织入到同一目标对象。
使用注解配置Spring AOP总体分为两步,第一步是在xml文件中声明激活自动扫描组件功能,同时激活自动代理功能(同时在xml中添加一个UserService的普通服务层组件,来测试AOP的注解功能):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 激活组件扫描功能,在包cn.ysh.studio.spring.aop及其子包下面自动扫描通过注解配置的组件 -->
<context:component-scan base-package="cn.ysh.studio.spring.aop"/>
<!-- 激活自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 用户服务对象 -->
<bean id="userService" class="cn.ysh.studio.spring.aop.service.UserService" />
</beans>
第二步是为Aspect切面类添加注解:参照上面的例子。
今天在看新工程代码的时候,发现数据源是多个,用了spring的AbstractRoutingDataSource实现动态数据源切换。就是在程序运行时,把数据源动态织入到程序中,从而选择读取主库还是从库。主要使用的技术是:annotation,Spring AOP ,反射。(注意这里的多数据源不牵扯到跨库的事物)。在网上找了篇类似的的实现,转载过来。
—————————————以下为转载—————————————————————
原文地址:http://linhongyu.blog.51cto.com/6373370/1615895
原文如下,为了便于阅读,有所改动:
扩展AbstractRoutingDataSource类,并重写其中的determineCurrentLookupKey()方法,来实现数据源的切换:
package com.datasource.test.util.database;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 获取数据源(依赖于spring)
* @author linhy
*/
public class DynamicDataSource extends AbstractRoutingDataSource{
@Override
protected Object determineCurrentLookupKey() {
return DataSourceHolder.getDataSource();
}
}DataSourceHolder这个类则是我们自己封装的对数据源进行操作的类:
package com.datasource.test.util.database;
/**
* 数据源操作
* @author linhy
*/
public class DataSourceHolder {
//线程本地环境
private static final ThreadLocal<String> dataSources = new ThreadLocal<String>();
//设置数据源
public static void setDataSource(String customerType) {
dataSources.set(customerType);
}
//获取数据源
public static String getDataSource() {
return (String) dataSources.get();
}
//清除数据源
public static void clearDataSource() {
dataSources.remove();
}
}
/** * 执行dao方法之前的切面 * 获取datasource对象之前往RouteHolder中指定当前线程数据源路由的key * @author Administrator * */ public class DataSourceAspect { /** * 在dao层方法之前获取datasource对象之前在切面中指定当前线程数据源路由的key */ public void beforeDaoMethod(JoinPoint point) { //dao方法上配置的注解 DataSource datasource = ((MethodSignature)point.getSignature()).getMethod().getAnnotation(DataSource.class); RouteHolder.setRouteKey(datasource.value()); } }业务逻辑,通常是涉及到数据源的dao.
@Named("userService")
public class UserService
{
@Inject
private UserDao userDao;
@DataSource("master")
@Transactional(propagation=Propagation.REQUIRED)
public void updatePasswd(int userid,String passwd)
{
User user = new User();
user.setUserid(userid);
user.setPassword(passwd);
userDao.updatePassword(user);
}
@DataSource("slave")
@Transactional(propagation=Propagation.REQUIRED)
public User getUser(int userid)
{
User user = userDao.getUserById(userid);
System.out.println("username------:"+user.getUsername());
return user;
}
}二、 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- Spring 数据库相关配置 放在这里 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> bean id="dataSource" class="com.westone.datasource.DbRouteDataSource">
<property name="targetDataSources">
<map>
<!-- write -->
<entry key="master" value-ref="master"></entry>
<!-- read -->
<entry key="slave" value-ref="slave"></entry>
</map>
</property>
</bean>
<bean id="master" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverclass}" />
<property name="url" value="${jdbc.masterurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<bean id="slave" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverclass}" />
<property name="url" value="${jdbc.slaveurl}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="${jdbc.maxActive}"></property>
<property name="maxIdle" value="${jdbc.maxIdle}"></property>
<property name="maxWait" value="${jdbc.maxWait}"></property>
</bean>
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" >
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:config/mybatis/mybatis.cfg.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置mapper的映射扫描器 根据包中定义的接口自动生成dao的实现类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.westone.dao"></property>
</bean>
<!-- 为业务逻辑层的方法解析@DataSource注解 为当前线程的routeholder注入数据源key -->
<bean id="aspectBean" class="com.westone.datasource.aspect.DataSourceAspect"></bean>
<aop:config>
<aop:aspect id="dataSourceAspect" ref="aspectBean">
<aop:pointcut id="dataSourcePoint" expression="execution(public * com.westone.service.*.*(..))" />
<aop:before method="beforeDaoMethod" pointcut-ref="dataSourcePoint"/>
</aop:aspect>
</aop:config>
<!-- 事务管理器配置 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解驱动 在业务逻辑层上使用@Transactional 注解 为业务逻辑层管理事务-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
最后测试业务逻辑层的方法发现 可以根据方法上的@DataSource("master") 注解配置不同的数据源key 使用动态数据源。 不需要再业务逻辑方法层中定义不同的数据源对象,人为的使用不同的数据源操作数据。
*********************原文结束********************
补充下背景知识:
切面(Aspect):官方的抽象定义为“一个关注点的模块化,这个关注点可能会横切多个对象”,在本例中,“切面”就是类TestAspect所关注的具体行为,例如,AServiceImpl.barA()的调用就是切面TestAspect所关注的行为之一。“切面”在ApplicationContext中<aop:aspect>来配置。
连接点(Joinpoint) :程序执行过程中的某一行为,例如,UserService.get的调用或者UserService.delete抛出异常等行为。
通知(Advice) :“切面”对于某个“连接点”所产生的动作,例如,TestAspect中对com.spring.service包下所有类的方法进行日志记录的动作就是一个Advice。其中,一个“切面”可以包含多个“Advice”,例如ServiceAspect。
切入点(Pointcut) :匹配连接点的断言,在AOP中通知和一个切入点表达式关联。例如,TestAspect中的所有通知所关注的连接点,都由切入点表达式execution(* com.spring.service.*.*(..))来决定。
目标对象(Target Object) :被一个或者多个切面所通知的对象。例如,AServcieImpl和BServiceImpl,当然在实际运行时,Spring AOP采用代理实现,实际AOP操作的是TargetObject的代理对象。
AOP代理(AOP Proxy) :在Spring AOP中有两种代理方式,JDK动态代理和CGLIB代理。默认情况下,TargetObject实现了接口时,则采用JDK动态代理,例如,AServiceImpl;反之,采用CGLIB代理,例如,BServiceImpl。强制使用CGLIB代理需要将 <aop:config>的 proxy-target-class属性设为true。
通知(Advice)类型:
前置通知(Before advice):在某连接点(JoinPoint)之前执行的通知,但这个通知不能阻止连接点前的执行。ApplicationContext中在<aop:aspect>里面使用<aop:before>元素进行声明。例如,TestAspect中的doBefore方法。
后置通知(After advice):当某连接点退出的时候执行的通知(不论是正常返回还是异常退出)。ApplicationContext中在<aop:aspect>里面使用<aop:after>元素进行声明。例如,ServiceAspect中的returnAfter方法,所以Teser中调用UserService.delete抛出异常时,returnAfter方法仍然执行。
返回后通知(After return advice):在某连接点正常完成后执行的通知,不包括抛出异常的情况。ApplicationContext中在<aop:aspect>里面使用<after-returning>元素进行声明。
环绕通知(Around advice):包围一个连接点的通知,类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为,也可以选择不执行。ApplicationContext中在<aop:aspect>里面使用<aop:around>元素进行声明。例如,ServiceAspect中的around方法。
抛出异常后通知(After throwing advice):在方法抛出异常退出时执行的通知。ApplicationContext中在<aop:aspect>里面使用<aop:after-throwing>元素进行声明。例如,ServiceAspect中的returnThrow方法。
注:可以将多个通知应用到一个目标对象上,即可以将多个切面织入到同一目标对象。
使用注解配置Spring AOP总体分为两步,第一步是在xml文件中声明激活自动扫描组件功能,同时激活自动代理功能(同时在xml中添加一个UserService的普通服务层组件,来测试AOP的注解功能):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 激活组件扫描功能,在包cn.ysh.studio.spring.aop及其子包下面自动扫描通过注解配置的组件 -->
<context:component-scan base-package="cn.ysh.studio.spring.aop"/>
<!-- 激活自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 用户服务对象 -->
<bean id="userService" class="cn.ysh.studio.spring.aop.service.UserService" />
</beans>
第二步是为Aspect切面类添加注解:参照上面的例子。
相关文章推荐
- 深入理解Java Hello World!程序
- Java NIO系列教程(六) Selector
- 55. spring boot 服务配置和部署【从零开始学Spring Boot】
- Springmvc 上传图片
- Java中的Random()函数
- 设计模式之观察者模式
- java多线程总结
- Spring MVC中文文档翻译发布
- java代码将地址转化为坐标的工具类
- Java Socket编程----通信是这样炼成的
- JAVA中NIO基础(一)
- Map的遍历
- 实用的java注解工具类
- [java]设计模式1-单例模式
- java--Collection和Collections
- 华为机试---奖学金
- JAVAweb 实现导入导出
- Java NIO系列教程(五) 通道之间的数据传输
- 【SpringMVC学习10】SpringMVC对RESTfull的支持
- 对JAVA RMI的认识