使用AOP 实现多数据源 切换
多数据源的实现,这里就来个实例吧
1、在 spring 的配置文件中数据源信息
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="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-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd"> <!-- 加载配置属性文件 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath:conf.properties" />
<!-- C3P0连接池配置 -->
<bean id="dataSource_zh_CN" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db1.jdbc.driver}" />
<property name="jdbcUrl" value="${db1.jdbc.url}" />
<property name="user" value="${db1.jdbc.username}" />
<property name="password" value="${db1.jdbc.password}" /> <!-- 配置初始化大小、最小、最大 -->
<property name="initialPoolSize" value="${jdbc.pool.init}" />
<property name="minPoolSize" value="${jdbc.pool.minIdle}" />
<property name="maxPoolSize" value="${jdbc.pool.maxActive}" />
<property name="maxIdleTime" value="60000" />
</bean> <!-- C3P0连接池配置 -->
<bean id="dataSource_en_US" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db2.jdbc.driver}" />
<property name="jdbcUrl" value="${db2.jdbc.url}" />
<property name="user" value="${db2.jdbc.username}" />
<property name="password" value="${db2.jdbc.password}" /> <!-- 配置初始化大小、最小、最大 -->
<property name="initialPoolSize" value="${jdbc.pool.init}" />
<property name="minPoolSize" value="${jdbc.pool.minIdle}" />
<property name="maxPoolSize" value="${jdbc.pool.maxActive}" />
<property name="maxIdleTime" value="60000" />
</bean> <bean id="dataSource" class="com.bkc.bpmp.core.datasource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="dataSource_zh_CN" value-ref="dataSource_zh_CN" />
<entry key="dataSource_en_US" value-ref="dataSource_en_US" />
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource_zh_CN" />
</bean> </beans>
2、在 Spring 配置文件中配置 AOP 切面信息,当访问 包 com.bkc.bpmp 及下面的子包中的类的方法时
<!-- 开启aop注解方式 -->
<aop:aspectj-autoproxy proxy-target-class="false"/> <bean id="dataSourceInterceptor" class="com.bkc.bpmp.core.aspectj.DataSourceInterceptor" /> <aop:config>
<aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">
<aop:pointcut id="setDs" expression="execution(* com.bkc.bpmp..*.*(..))" />
<aop:before method="setdataSourceCnOrEn" pointcut-ref="setDs"/>
</aop:aspect>
</aop:config>
3、实现 数据源 切面切换的方法,这里假设是根据 locale 来切换数据源的
package com.bkc.bpmp.core.aspectj; import java.lang.reflect.Method; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder; import com.bkc.bpmp.core.annotation.DbType;
import com.bkc.bpmp.core.contants.DbTypeEnum;
import com.bkc.bpmp.core.datasource.DatabaseContextHolder; /**
* 用于实现 动态数据库切换的AOP 方法类
*
* @author ppnie
*/
public class DataSourceInterceptor
{
/**
* 设置数据源为中文 还是 英文
* @param jp
*/
public void setdataSourceCnOrEn(JoinPoint jp) { String curLocale = LocaleContextHolder.getLocale().toString();
System.out.println("==================dataSource_"+curLocale);
if(curLocale.equals("en_US"))
{
DatabaseContextHolder.setCustomerType("dataSource_"+curLocale);
}
else
{
DatabaseContextHolder.setCustomerType("dataSource_zh_CN");
}
logger.info("======="+"dataSource_"+curLocale+"======="); }
}
4、自定义动态数据源的切换方法,主要就是 写一个 AbstractRoutingDataSource 的子类啦
package com.bkc.bpmp.core.datasource; public class DatabaseContextHolder
{ private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); public static void setCustomerType(String customerType)
{
contextHolder.set(customerType);
} public static String getCustomerType()
{
return contextHolder.get();
} public static void clearCustomerType()
{
contextHolder.remove();
}
}
package com.bkc.bpmp.core.datasource; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DynamicDataSource extends AbstractRoutingDataSource
{ @Override
protected Object determineCurrentLookupKey()
{
return DatabaseContextHolder.getCustomerType();
} }
就这样,达到触发条件的时候,就会去切换数据库啦
使用AOP 实现多数据源 切换的更多相关文章
- 【原】继承AbstractRoutingDataSource再通过AOP实现动态数据源切换
关于AbstractRoutingDataSource动态切换数据源是我在研究某开源项目时候才注意到的,大概就看懂了Spring AOP切面这里,根据作者的意思是通过继承这个抽象类可以实现数据源的动态 ...
- 继承AbstractRoutingDataSource再通过AOP实现动态数据源切换
package com.zdd.data.aspect; import java.util.ArrayList; import java.util.HashMap; import java.util. ...
- 继承AbstractRoutingDataSource再通过AOP实现动态数据源切换(转)
关于AbstractRoutingDataSource我在研究开源中国的开源项目时候才发现,好奇的看了一下代码发现自己看不明白,大概就看懂了Spring AOP切面这里,根据注释作者的意思是通过这个可 ...
- 【开发笔记】- AbstractRoutingDataSource动态数据源切换,AOP实现动态数据源切换
AbstractRoutingDataSource动态数据源切换 上周末,室友通宵达旦的敲代码处理他的多数据源的问题,搞的非常的紧张,也和我聊了聊天,大概的了解了他的业务的需求.一般的情况下我们都是使 ...
- AbstractRoutingDataSource动态数据源切换,AOP实现动态数据源切换
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/u012881904/article/de ...
- Java:基于AOP的动态数据源切换(附源码)
1 动态数据源的必要性 我们知道,物理服务机的CPU.内存.存储空间.连接数等资源都是有限的,某个时段大量连接同时执行操作,会导致数据库在处理上遇到性能瓶颈.而在复杂的互联网业务场景下,系统流量日益膨 ...
- Spring主从数据库的配置和动态数据源切换原理
原文:https://www.liaoxuefeng.com/article/00151054582348974482c20f7d8431ead5bc32b30354705000 在大型应用程序中,配 ...
- SpringBoot31 整合SpringJDBC、整合MyBatis、利用AOP实现多数据源
一.整合SpringJDBC 1 JDBC JDBC(Java Data Base Connectivity,Java 数据库连接)是一种用于执行 SQL 语句的 Java API,可以为多种关系数 ...
- dubbo服务+Spring事务+AOP动态数据源切换 出错
1:问题描述,以及分析 项目用了spring数据源动态切换,服务用的是dubbo.在运行一段时间后程序异常,更新操作没有切换到主库上. 这个问题在先调用读操作后再调用写操作会出现. 经日志分析原因: ...
随机推荐
- XBOX ONE游戏开发常见问题
XBOX ONE游戏开发常见问题 终于弄懂这个在Unity的sdk在Account Picker切换账号的机制了,一个手柄注册一个账号,在游戏里面的时候,只有另外一个手柄选择自己的账号,系统的Acti ...
- 一行代码远离Google浏览器兼容问题的困扰
跟Html相比,Web Flash开发的最大优势就在于兼容性好,因为FlashPlayer的开发商只有Adobe一家.但自从Google插了一脚进来,改版出自己的FlashPlayer以后,这一优势就 ...
- cookie与session的爱恨情仇
这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...
- 队列Queue
java中,Queue是Collection接口的子接口,Queue的实现类很多,如LinkedList类. 实际使用可以用LinkedList写一个Queue类,实现入队.出队.求队长.判空.打印等 ...
- AngularJs 1.5 $location获取url参数
地址:http://localhost/waservice.html?id=17 获取参数id的值 app.config(['$locationProvider', function ($locati ...
- django复习笔记3:urls/views/templates三板斧
0.先看看文件结构 mysite/ mysite/ ├── __pycache__ │ └── manage.cpython-.pyc ├── blog │ ├── __init__.py │ ...
- Kali linux渗透测试常用工具汇总1
1.ProxyChains 简介:代理工具.支持HTTP/SOCKS4/SOCK5的代理服务器,允许TCP/DNS通过代理隧道. 应用场景:通过代理服务器上网. 配置:/etc/proxychains ...
- js双层动画幻灯
js双层动画幻灯 点击下载
- 安装Docker Toolbox后出现的问题
Installing Docker Toolbox on Windows with Hyper-V Installed Installing Docker on Windows is a fairly ...
- 职责链(Chain of Responsibility)模式在航空货运中的运用实例
设计模式这东西,基本上属于“看懂一瞬间,用会好几年”.只有实际开发中,当某一模式很好的满足了业务需求时,才会有真切的感觉.借用一句<闪电侠>中,绿箭侠教导闪电侠的台词:“不是你碰巧遇到了它 ...