作者 RuoYi

reset dataSourceAspect

1 package com.ruoyi.framework.aspectj; 1 package com.ruoyi.framework.aspectj;
2 2
  3 +import java.util.Objects;
3 import org.aspectj.lang.ProceedingJoinPoint; 4 import org.aspectj.lang.ProceedingJoinPoint;
4 import org.aspectj.lang.annotation.Around; 5 import org.aspectj.lang.annotation.Around;
5 import org.aspectj.lang.annotation.Aspect; 6 import org.aspectj.lang.annotation.Aspect;
  7 +import org.aspectj.lang.annotation.Pointcut;
  8 +import org.aspectj.lang.reflect.MethodSignature;
6 import org.slf4j.Logger; 9 import org.slf4j.Logger;
7 import org.slf4j.LoggerFactory; 10 import org.slf4j.LoggerFactory;
  11 +import org.springframework.core.annotation.AnnotationUtils;
8 import org.springframework.core.annotation.Order; 12 import org.springframework.core.annotation.Order;
9 import org.springframework.stereotype.Component; 13 import org.springframework.stereotype.Component;
10 import com.ruoyi.common.annotation.DataSource; 14 import com.ruoyi.common.annotation.DataSource;
  15 +import com.ruoyi.common.utils.StringUtils;
11 import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder; 16 import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
12 17
13 /** 18 /**
@@ -22,11 +27,22 @@ public class DataSourceAspect @@ -22,11 +27,22 @@ public class DataSourceAspect
22 { 27 {
23 protected Logger logger = LoggerFactory.getLogger(getClass()); 28 protected Logger logger = LoggerFactory.getLogger(getClass());
24 29
25 - @Around("@annotation(dataSource) || @within(dataSource)")  
26 - public Object around(ProceedingJoinPoint point, DataSource dataSource) throws Throwable 30 + @Pointcut("@annotation(com.ruoyi.common.annotation.DataSource)"
  31 + + "|| @within(com.ruoyi.common.annotation.DataSource)")
  32 + public void dsPointCut()
27 { 33 {
28 34
29 - DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name()); 35 + }
  36 +
  37 + @Around("dsPointCut()")
  38 + public Object around(ProceedingJoinPoint point) throws Throwable
  39 + {
  40 + DataSource dataSource = getDataSource(point);
  41 +
  42 + if (StringUtils.isNotNull(dataSource))
  43 + {
  44 + DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
  45 + }
30 46
31 try 47 try
32 { 48 {
@@ -39,4 +55,18 @@ public class DataSourceAspect @@ -39,4 +55,18 @@ public class DataSourceAspect
39 } 55 }
40 } 56 }
41 57
  58 + /**
  59 + * 获取需要切换的数据源
  60 + */
  61 + public DataSource getDataSource(ProceedingJoinPoint point)
  62 + {
  63 + MethodSignature signature = (MethodSignature) point.getSignature();
  64 + DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);
  65 + if (Objects.nonNull(dataSource))
  66 + {
  67 + return dataSource;
  68 + }
  69 +
  70 + return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);
  71 + }
42 } 72 }
@@ -4,10 +4,8 @@ import java.lang.reflect.Method; @@ -4,10 +4,8 @@ import java.lang.reflect.Method;
4 import java.util.Collections; 4 import java.util.Collections;
5 import java.util.List; 5 import java.util.List;
6 import org.aspectj.lang.JoinPoint; 6 import org.aspectj.lang.JoinPoint;
7 -import org.aspectj.lang.Signature;  
8 import org.aspectj.lang.annotation.Aspect; 7 import org.aspectj.lang.annotation.Aspect;
9 import org.aspectj.lang.annotation.Before; 8 import org.aspectj.lang.annotation.Before;
10 -import org.aspectj.lang.annotation.Pointcut;  
11 import org.aspectj.lang.reflect.MethodSignature; 9 import org.aspectj.lang.reflect.MethodSignature;
12 import org.slf4j.Logger; 10 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory; 11 import org.slf4j.LoggerFactory;