星空网 > 软件开发 > Java

第九章 springboot + mybatis + 多数据源 (AOP实现)

在第八章 springboot + mybatis + 多数据源代码的基础上,做两点修改

1、ShopDao

第九章 springboot + mybatis + 多数据源 (AOP实现)第九章 springboot + mybatis + 多数据源 (AOP实现)
 1 package com.xxx.firstboot.dao; 2  3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.stereotype.Repository; 5  6 import com.xxx.firstboot.domain.Shop; 7 import com.xxx.firstboot.mapper.ShopMapper; 8  9 @Repository10 public class ShopDao {11   @Autowired12   private ShopMapper mapper;13 14   /**15    * 获取shop16   */17   public Shop getShop(int id) {18     return mapper.getShop(id);19   }20 }

View Code

说明:只是去掉了设置数据源key的那一句代码

 

2、DataSourceAspect

第九章 springboot + mybatis + 多数据源 (AOP实现)第九章 springboot + mybatis + 多数据源 (AOP实现)
 1 package com.xxx.firstboot.common.datasource; 2  3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.Aspect; 5 import org.aspectj.lang.annotation.Before; 6 import org.springframework.stereotype.Component; 7  8 import com.xxx.firstboot.dao.ShopDao; 9 10 @Aspect11 @Component12 public class DataSourceAspect {13   14   @Before("execution(* com.xxx.firstboot.dao.*.*(..))")15   public void setDataSourceKey(JoinPoint point){16     //连接点所属的类实例是ShopDao17     if(point.getTarget() instanceof ShopDao){18       DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);19     }else{//连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)20       DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);21     }22   }23   24 //  @Around("execution(* com.xxx.firstboot.dao.*.*(..))")25 //  public Object setDataSourceKeyByAround(ProceedingJoinPoint point) throws Throwable{26 //    if(point.getTarget() instanceof ShopDao){27 //      DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);28 //    }else{//连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)29 //      DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);30 //    }31 //    return point.proceed();//执行目标方法32 //  }33   34 }

View Code

说明:列出了两种切面方法,在这里推荐使用前者,原因:

  • @Around:需要写执行目标方法的那一行代码,而这一行代码可能会抛异常,还需要抛出或捕获

对于切点表达式,可以抽取出来,进行重复利用。如上代码可以改为如下:

第九章 springboot + mybatis + 多数据源 (AOP实现)第九章 springboot + mybatis + 多数据源 (AOP实现)
 1 package com.xxx.firstboot.common.datasource; 2  3 import org.aspectj.lang.JoinPoint; 4 import org.aspectj.lang.annotation.Aspect; 5 import org.aspectj.lang.annotation.Before; 6 import org.aspectj.lang.annotation.Pointcut; 7 import org.springframework.stereotype.Component; 8  9 import com.xxx.firstboot.dao.ShopDao;10 11 @Aspect12 @Component13 public class DataSourceAspect {14 15   /**16    * 使用空方法定义切点表达式17   */18   @Pointcut("execution(* com.xxx.firstboot.dao.*.*(..))")19   public void declareJointPointExpression() {20   }21 22   /**23    * 使用定义切点表达式的方法进行切点表达式的引入24   */25   @Before("declareJointPointExpression()")26   public void setDataSourceKey(JoinPoint point) {27     // 连接点所属的类实例是ShopDao28     if (point.getTarget() instanceof ShopDao) {29       DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb2);30     } else {// 连接点所属的类实例是UserDao(当然,这一步也可以不写,因为defaultTargertDataSource就是该类所用的mytestdb)31       DatabaseContextHolder.setDatabaseType(DatabaseType.mytestdb);32     }33   }34 35 }

View Code

注意:该切点表达式也可以用在其他切面类中,引入的时候使用"全类名.切点方法名()",例:@Before("com.xxx.firstboot.common.datasource.DataSourceAspect.declareJointPointExpression()")

 

关于AOP,查看:第一章 AOP




原标题:第九章 springboot + mybatis + 多数据源 (AOP实现)

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

德国法国包装法:https://www.goluckyvip.com/tag/59611.html
法国德国包装法:https://www.goluckyvip.com/tag/59612.html
德国包装法针对什么产品:https://www.goluckyvip.com/tag/59613.html
德国包装法图标:https://www.goluckyvip.com/tag/59614.html
德国包装法流程:https://www.goluckyvip.com/tag/59616.html
德国包装法是什么:https://www.goluckyvip.com/tag/59617.html
商机!速卖通推出小B采销节,挖掘机、莫桑钻、电动车近期在海外走俏!:https://www.xlkjsw.com/news/93297.html
Zalando对欧盟在线平台监管费提出质疑:https://www.kjdsnews.com/a/1842241.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流