赵计刚
每天进步一点点

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

1、ShopDao

 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 @Repository
10 public class ShopDao {
11     @Autowired
12     private ShopMapper mapper;
13 
14     /**
15      * 获取shop
16      */
17     public Shop getShop(int id) {
18         return mapper.getShop(id);
19     }
20 }
View Code

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

 

2、DataSourceAspect

 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 @Aspect
11 @Component
12 public class DataSourceAspect {
13     
14     @Before("execution(* com.xxx.firstboot.dao.*.*(..))")
15     public void setDataSourceKey(JoinPoint point){
16         //连接点所属的类实例是ShopDao
17         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:需要写执行目标方法的那一行代码,而这一行代码可能会抛异常,还需要抛出或捕获

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

 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 @Aspect
12 @Component
13 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         // 连接点所属的类实例是ShopDao
28         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

posted on 2016-04-21 10:19  赵计刚  阅读(18772)  评论(3编辑  收藏  举报