spring中怎么通过注解切换多数据源
发表于:2025-12-01 作者:千家信息网编辑
千家信息网最后更新 2025年12月01日,spring中怎么通过注解切换多数据源,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。第一步,配置数据源
千家信息网最后更新 2025年12月01日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 ThreadLocal threadLocal = 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(){ List d = 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安全错误
数据库的锁怎样保障安全
贵州热梦网络技术有限公司董事长
中国石化电子邮箱服务器
网络安全管理人员培训考试题
网络安全是什么的组成
网络安全服务考核要求
t140服务器bios
网络安全软件的使用实验报告
双色球如何看到售票数据库
融媒网络安全
金蝶软件如何导入数据库
服务器做私服
强两会期间网络安全
黄浦租房网络安全
软件开发的开票项目有哪些
服务器能放在纸壳上吗
登录服务器无响应
软件开发费计入无形资产吗
关于网络安全的英语专题讲座
安装了mysql的数据库
计算机网络安全使用承诺书
front怎么连接数据库
sql数据库受欢迎吗
广州幻象引擎网络技术
数据库表丢失恢复
航空工业出版社计算机网络技术
浙江大型软件开发行业
数据库中的long类型
什么是服务器ip地址
系统正版化和网络安全
校时服务器的参数