spring中怎么通过注解切换多数据源
发表于:2024-09-22 作者:千家信息网编辑
千家信息网最后更新 2024年09月22日,spring中怎么通过注解切换多数据源,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。第一步,配置数据源
千家信息网最后更新 2024年09月22日spring中怎么通过注解切换多数据源
spring中怎么通过注解切换多数据源,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
第一步,配置数据源
第二步,定义用来切库的注解,和枚举类
@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public @interface Dataswitch { Datatype value() default Datatype.master;}public enum Datatype { master("masterDataSource"),slave("slaveDataSource"); private String value; Datatype(String name){ this.value = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; }}
第三步,定义一个当前线程的变量的工具类,用于设置对应的数据源名称
public class DynamicDataSourceHolder { private static final ThreadLocalthreadLocal = new ThreadLocal (); public static String getThreadLocal() { return threadLocal.get(); } public static void setThreadLocal(String name) { threadLocal.set(name); } public static void clear(){ threadLocal.remove(); }}
第四步,创建AbstactRoutingDataSource的子类,重写determineCurrentLockupKey方法
public class DynamicDataSource extends AbstractRoutingDataSource{ protected Object determineCurrentLookupKey() { System.out.println(DynamicDataSourceHolder.getThreadLocal()); return DynamicDataSourceHolder.getThreadLocal(); }}
第五步,将多数据源配置到用我们创建的DynamicDataSource
第六步,配置切面,在操作数据库方法之前,获取注解配置的数据源名称,返回
@Component@Aspect@Order(0)public class DataSourceAspect { @Pointcut("execution (* xin.youhuila.sorceswitch.service..*(..))") public void aspect(){ } @Before("aspect()") public void before(JoinPoint joinPoint){ Class> clazz = joinPoint.getTarget().getClass(); Method[] method = clazz.getMethods(); Dataswitch dataswitch = null; boolean is = false; for(Method m:method){//此处最好改成通过签名获取调用方法是否含有注解,而不是遍历每个方法,可参考http://www.gitout.cn/?p=2398 if(m.isAnnotationPresent(Dataswitch.class)){ dataswitch = m.getAnnotation(Dataswitch.class); DynamicDataSourceHolder.setThreadLocal(dataswitch.value().getValue()); is = true; } } if(!is){ DynamicDataSourceHolder.setThreadLocal(Datatype.master.getValue()); } } @After("aspect()") public void after(){ DynamicDataSourceHolder.clear(); }}
第七步,使用
@Servicepublic class DemoService { @Autowired DemoMapper demoMapper; @Dataswitch(Datatype.master) public void select(){ Listd = demoMapper.select(); for(Demo demo:d){ System.out.println(demo); } }}
--------------------------------http://www.gitout.cn/?p=2398文章实现-----------------------
/** * 数据源切面 */@Aspect@Componentpublic class DynamicDataSourceAspect { @Pointcut("@annotation(com...datasource.DynamicDataSourceAnnotation)") public void pointCut() { } @Before("pointCut()") public void testBefore(JoinPoint point) { // 获得当前访问的class Class> className = point.getTarget().getClass(); DynamicDataSourceAnnotation dataSourceAnnotation = className.getAnnotation(DynamicDataSourceAnnotation.class); String dataSource = DataSourceConst.DB_ACTIVITY; // 优先级: 方法 > 类 > DB_ACTIVITY if(dataSourceAnnotation != null) { dataSource = dataSourceAnnotation.dataSource(); } String methodName = point.getSignature().getName(); // 得到方法的参数的类型 Class>[] argClass = ((MethodSignature) point.getSignature()).getParameterTypes(); try { Method method = className.getMethod(methodName, argClass); if (method.isAnnotationPresent(DynamicDataSourceAnnotation.class)) { DynamicDataSourceAnnotation annotation = method.getAnnotation(DynamicDataSourceAnnotation.class); dataSource = annotation.dataSource(); } } catch (Exception e) { e.printStackTrace(); } DataSourceContextHolder.setDataSourceType(dataSource); } @After("pointCut()") public void testAfter(JoinPoint point) { // 获得当前访问的class Class> className = point.getTarget().getClass(); DynamicDataSourceAnnotation dataSourceAnnotation = className.getAnnotation(DynamicDataSourceAnnotation.class); if (dataSourceAnnotation != null) { // 获得访问的方法名 String methodName = point.getSignature().getName(); // 得到方法的参数的类型 Class>[] argClass = ((MethodSignature) point.getSignature()).getParameterTypes(); String dataSource = DataSourceConst.DB_ACTIVITY; try { Method method = className.getMethod(methodName, argClass); if (method.isAnnotationPresent(DynamicDataSourceAnnotation.class)) { DynamicDataSourceAnnotation annotation = method.getAnnotation(DynamicDataSourceAnnotation.class); dataSource = annotation.dataSource(); } } catch (Exception e) { e.printStackTrace(); } if (dataSource != null && !DataSourceConst.DB_ACTIVITY.equals(dataSource)) { DataSourceContextHolder.clearDataSourceType(); } } }}
关于spring中怎么通过注解切换多数据源问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。
方法
数据
数据源
注解
配置
问题
切换
切面
参数
名称
更多
类型
帮助
解答
易行
简单易行
优先级
内容
变量
子类
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
宏杰服务器
扬州软件开发有哪些
吴中区税务局安全服务器地址
配置一个安装服务器失败
贵州网络安全技能竞赛
止泻药广告数据库
通信核心网络技术
ntp服务器 下载
数据库最新技术有哪些
liunx数据库在哪
刀剑大作战无法连接服务器
二手服务器回收图
数据库为何无法编辑关系
网络安全体系的恢复
我的世界服务器玩家该如何封号
会议电视网络技术体制
深圳集云网络技术有限公司
数据库的代理作业不能执行
如何修改数据库的地址
惠普服务器阵列卡换电池
软件开发品牌大全
互联网促进科技进步的例子
青岛 物联网软件开发公司
拼多多电话采集软件开发
打印服务器工具
手机91vpn服务器
东方通与网络安全审查办法
中学生与网络安全教育教案
锐速科技服务器
计算机网络技术专业课配电脑吗