Chapter 9 springboot + mybatis + multiple data sources (AOP implementation) and springbootmybatis
On the basis of Chapter 8 springboot + mybatis + multi-data source code, make two changes
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 @ Repository10 public class ShopDao {11 @ Autowired12 private ShopMapper mapper; 13 14/*** 15 * Get shop16 */17 public Shop getShop (int id) {18 return mapper. getShop (id); 19} 20}View Code
Note: The code for setting the data source key is removed.
2. performanceaspect
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 performanceaspect {13 14 @ Before ("execution (* com. xxx. firstboot. dao. *. *(..)) ") 15 public void setDataSourceKey (JoinPoint point) {16 // The class instance to which the connection point belongs is ShopDao17 if (point. getTarget () instanceof ShopDao) {18 DatabaseContextHolder. setDatabaseType (DatabaseType. mytestdb2); 19} else {// The class instance to which the connection point belongs is UserDao (of course, this step can also be left blank, because defaultTargertDataSource is the mytestdb used by this class) 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 {// The class instance to which the connection point belongs is UserDao (of course, this step can also be left blank, because defaultTargertDataSource is the mytestdb used by this class) 29 // DatabaseContextHolder. setDatabaseType (DatabaseType. mytestdb); 30 //} 31 // return point. proceed (); // execution target method 32 //} 33 34}View Code
Note: Two methods are listed. The former method is recommended here because:
- @ Around: the line of code that needs to be written to execute the target method. This line of code may throw an exception and need to be thrown or captured.
For the cut-point expression, it can be extracted for reuse. The code above can be changed to the following:
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 performanceaspect {14 15/** 16 * use an empty method to define the cut point expression 17 */18 @ Pointcut ("execution (* com. xxx. firstboot. dao. *. *(..)) ") 19 public void declareJointPointExpression () {20} 21 22/** 23 * use the Method Defining the cut point expression to introduce the cut point expression 24 */25 @ Before (" declareJointPointExpression () ") 26 public void setDataSourceKey (JoinPoint point) {27 // The class instance to which the connection point belongs is ShopDao28 if (point. getTarget () instanceof ShopDao) {29 DatabaseContextHolder. setDatabaseType (DatabaseType. mytestdb2); 30} else {// The class instance to which the connection point belongs is UserDao (of course, this step can also be left blank, because defaultTargertDataSource is the mytestdb used by this class) 31 DatabaseContextHolder. setDatabaseType (DatabaseType. mytestdb); 32} 33} 34 35}View Code
Note: This cut-point expression can also be used in other face-cutting classes, and "full class name" is used for introduction. cut Point Method Name () ", for example: @ Before (" com. xxx. firstboot. common. datasource. performanceaspect. declareJointPointExpression ()")
For more information about AOP, see Chapter 1 AOP