From c99b9262a8ec9f826684c70fb4fce68ca85ceaab Mon Sep 17 00:00:00 2001
From: 疯狂的狮子li <15040126243@163.com>
Date: 星期六, 18 九月 2021 18:05:50 +0800
Subject: [PATCH] update 优化aop写法 使用spring自动注入注解参数
---
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java | 26 ----
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java | 30 -----
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java | 36 ------
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java | 148 +++++++++--------------------
ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java | 28 -----
5 files changed, 57 insertions(+), 211 deletions(-)
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
index a2c101e..e4a6cdc 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataScopeAspect.java
@@ -9,14 +9,10 @@
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import org.aspectj.lang.JoinPoint;
-import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
-import org.aspectj.lang.annotation.Pointcut;
-import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
-import java.lang.reflect.Method;
import java.util.Map;
/**
@@ -58,23 +54,13 @@
*/
public static final String DATA_SCOPE = "dataScope";
- // 閰嶇疆缁囧叆鐐�
- @Pointcut("@annotation(com.ruoyi.common.annotation.DataScope)")
- public void dataScopePointCut() {
- }
-
- @Before("dataScopePointCut()")
- public void doBefore(JoinPoint point) throws Throwable {
+ @Before("@annotation(controllerDataScope)")
+ public void doBefore(JoinPoint point, DataScope controllerDataScope) throws Throwable {
clearDataScope(point);
- handleDataScope(point);
+ handleDataScope(point, controllerDataScope);
}
- protected void handleDataScope(final JoinPoint joinPoint) {
- // 鑾峰緱娉ㄨВ
- DataScope controllerDataScope = getAnnotationLog(joinPoint);
- if (controllerDataScope == null) {
- return;
- }
+ protected void handleDataScope(final JoinPoint joinPoint, DataScope controllerDataScope) {
// 鑾峰彇褰撳墠鐨勭敤鎴�
LoginUser loginUser = SecurityUtils.getLoginUser();
if (StringUtils.isNotNull(loginUser)) {
@@ -131,20 +117,6 @@
if (StringUtils.isNotBlank(sqlString.toString())) {
putDataScope(joinPoint, sqlString.substring(4));
}
- }
-
- /**
- * 鏄惁瀛樺湪娉ㄨВ锛屽鏋滃瓨鍦ㄥ氨鑾峰彇
- */
- private DataScope getAnnotationLog(JoinPoint joinPoint) {
- Signature signature = joinPoint.getSignature();
- MethodSignature methodSignature = (MethodSignature) signature;
- Method method = methodSignature.getMethod();
-
- if (method != null) {
- return method.getAnnotation(DataScope.class);
- }
- return null;
}
/**
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java
index 47e7742..55ec845 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/DataSourceAspect.java
@@ -6,33 +6,21 @@
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Pointcut;
-import org.aspectj.lang.reflect.MethodSignature;
-import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
-
-import java.util.Objects;
/**
* 澶氭暟鎹簮澶勭悊
*
- * @author ruoyi
+ * @author Lion Li
*/
@Aspect
@Order(-500)
@Component
public class DataSourceAspect {
- @Pointcut("@annotation(com.ruoyi.common.annotation.DataSource)"
- + "|| @within(com.ruoyi.common.annotation.DataSource)")
- public void dsPointCut() {
- }
-
- @Around("dsPointCut()")
- public Object around(ProceedingJoinPoint point) throws Throwable {
- DataSource dataSource = getDataSource(point);
-
+ @Around("@annotation(dataSource) || @within(dataSource)")
+ public Object around(ProceedingJoinPoint point, DataSource dataSource) throws Throwable {
if (StringUtils.isNotNull(dataSource)) {
DynamicDataSourceContextHolder.poll();
String source = dataSource.value().getSource();
@@ -47,16 +35,4 @@
}
}
- /**
- * 鑾峰彇闇�瑕佸垏鎹㈢殑鏁版嵁婧�
- */
- public DataSource getDataSource(ProceedingJoinPoint point) {
- MethodSignature signature = (MethodSignature) point.getSignature();
- DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);
- if (Objects.nonNull(dataSource)) {
- return dataSource;
- }
-
- return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);
- }
}
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java
index 246e428..f18d79b 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/LogAspect.java
@@ -1,11 +1,11 @@
package com.ruoyi.framework.aspectj;
import com.ruoyi.common.annotation.Log;
+import com.ruoyi.common.core.domain.dto.OperLogDTO;
import com.ruoyi.common.core.domain.model.LoginUser;
+import com.ruoyi.common.core.service.OperLogService;
import com.ruoyi.common.enums.BusinessStatus;
import com.ruoyi.common.enums.HttpMethod;
-import com.ruoyi.common.core.service.OperLogService;
-import com.ruoyi.common.core.domain.dto.OperLogDTO;
import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
@@ -13,12 +13,9 @@
import com.ruoyi.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
-import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
-import org.aspectj.lang.annotation.Pointcut;
-import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
@@ -26,60 +23,42 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;
/**
* 鎿嶄綔鏃ュ織璁板綍澶勭悊
- *
- * @author ruoyi
+ *
+ * @author Lion Li
*/
@Slf4j
@Aspect
@Component
-public class LogAspect
-{
-
- // 閰嶇疆缁囧叆鐐�
- @Pointcut("@annotation(com.ruoyi.common.annotation.Log)")
- public void logPointCut()
- {
- }
+public class LogAspect {
/**
* 澶勭悊瀹岃姹傚悗鎵ц
*
* @param joinPoint 鍒囩偣
*/
- @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
- public void doAfterReturning(JoinPoint joinPoint, Object jsonResult)
- {
- handleLog(joinPoint, null, jsonResult);
+ @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
+ public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult) {
+ handleLog(joinPoint, controllerLog, null, jsonResult);
}
/**
* 鎷︽埅寮傚父鎿嶄綔
- *
+ *
* @param joinPoint 鍒囩偣
- * @param e 寮傚父
+ * @param e 寮傚父
*/
- @AfterThrowing(value = "logPointCut()", throwing = "e")
- public void doAfterThrowing(JoinPoint joinPoint, Exception e)
- {
- handleLog(joinPoint, e, null);
+ @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
+ public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e) {
+ handleLog(joinPoint, controllerLog, e, null);
}
- protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult)
- {
- try
- {
- // 鑾峰緱娉ㄨВ
- Log controllerLog = getAnnotationLog(joinPoint);
- if (controllerLog == null)
- {
- return;
- }
+ protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult) {
+ try {
// 鑾峰彇褰撳墠鐨勭敤鎴�
LoginUser loginUser = SecurityUtils.getLoginUser();
@@ -91,13 +70,11 @@
String ip = ServletUtils.getClientIP();
operLog.setOperIp(ip);
operLog.setOperUrl(ServletUtils.getRequest().getRequestURI());
- if (loginUser != null)
- {
+ if (loginUser != null) {
operLog.setOperName(loginUser.getUsername());
}
- if (e != null)
- {
+ if (e != null) {
operLog.setStatus(BusinessStatus.FAIL.ordinal());
operLog.setErrorMsg(StringUtils.substring(e.getMessage(), 0, 2000));
}
@@ -110,10 +87,8 @@
// 澶勭悊璁剧疆娉ㄨВ涓婄殑鍙傛暟
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
// 淇濆瓨鏁版嵁搴�
- SpringUtils.getBean(OperLogService.class).recordOper(operLog);
- }
- catch (Exception exp)
- {
+ SpringUtils.getBean(OperLogService.class).recordOper(operLog);
+ } catch (Exception exp) {
// 璁板綍鏈湴寮傚父鏃ュ織
log.error("==鍓嶇疆閫氱煡寮傚父==");
log.error("寮傚父淇℃伅:{}", exp.getMessage());
@@ -123,13 +98,12 @@
/**
* 鑾峰彇娉ㄨВ涓鏂规硶鐨勬弿杩颁俊鎭� 鐢ㄤ簬Controller灞傛敞瑙�
- *
- * @param log 鏃ュ織
+ *
+ * @param log 鏃ュ織
* @param operLog 鎿嶄綔鏃ュ織
* @throws Exception
*/
- public void getControllerMethodDescription(JoinPoint joinPoint, Log log, OperLogDTO operLog, Object jsonResult) throws Exception
- {
+ public void getControllerMethodDescription(JoinPoint joinPoint, Log log, OperLogDTO operLog, Object jsonResult) throws Exception {
// 璁剧疆action鍔ㄤ綔
operLog.setBusinessType(log.businessType().ordinal());
// 璁剧疆鏍囬
@@ -137,68 +111,44 @@
// 璁剧疆鎿嶄綔浜虹被鍒�
operLog.setOperatorType(log.operatorType().ordinal());
// 鏄惁闇�瑕佷繚瀛榬equest锛屽弬鏁板拰鍊�
- if (log.isSaveRequestData())
- {
+ if (log.isSaveRequestData()) {
// 鑾峰彇鍙傛暟鐨勪俊鎭紝浼犲叆鍒版暟鎹簱涓��
setRequestValue(joinPoint, operLog);
}
// 鏄惁闇�瑕佷繚瀛榬esponse锛屽弬鏁板拰鍊�
- if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult))
- {
+ if (log.isSaveResponseData() && StringUtils.isNotNull(jsonResult)) {
operLog.setJsonResult(StringUtils.substring(JsonUtils.toJsonString(jsonResult), 0, 2000));
}
}
/**
* 鑾峰彇璇锋眰鐨勫弬鏁帮紝鏀惧埌log涓�
- *
+ *
* @param operLog 鎿嶄綔鏃ュ織
* @throws Exception 寮傚父
*/
- private void setRequestValue(JoinPoint joinPoint, OperLogDTO operLog) throws Exception
- {
+ private void setRequestValue(JoinPoint joinPoint, OperLogDTO operLog) throws Exception {
String requestMethod = operLog.getRequestMethod();
- if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod))
- {
+ if (HttpMethod.PUT.name().equals(requestMethod) || HttpMethod.POST.name().equals(requestMethod)) {
String params = argsArrayToString(joinPoint.getArgs());
operLog.setOperParam(StringUtils.substring(params, 0, 2000));
- }
- else
- {
+ } else {
Map<?, ?> paramsMap = (Map<?, ?>) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
operLog.setOperParam(StringUtils.substring(paramsMap.toString(), 0, 2000));
}
}
/**
- * 鏄惁瀛樺湪娉ㄨВ锛屽鏋滃瓨鍦ㄥ氨鑾峰彇
- */
- private Log getAnnotationLog(JoinPoint joinPoint) throws Exception
- {
- Signature signature = joinPoint.getSignature();
- MethodSignature methodSignature = (MethodSignature) signature;
- Method method = methodSignature.getMethod();
-
- if (method != null)
- {
- return method.getAnnotation(Log.class);
- }
- return null;
- }
-
- /**
* 鍙傛暟鎷艰
*/
- private String argsArrayToString(Object[] paramsArray)
- {
+ private String argsArrayToString(Object[] paramsArray) {
StringBuilder params = new StringBuilder();
- if (paramsArray != null && paramsArray.length > 0)
- {
- for (Object o : paramsArray) {
- if (StringUtils.isNotNull(o) && !isFilterObject(o)) {
- params.append(JsonUtils.toJsonString(o)).append(" ");
- }
- }
+ if (paramsArray != null && paramsArray.length > 0) {
+ for (Object o : paramsArray) {
+ if (StringUtils.isNotNull(o) && !isFilterObject(o)) {
+ params.append(JsonUtils.toJsonString(o)).append(" ");
+ }
+ }
}
return params.toString().trim();
}
@@ -210,27 +160,21 @@
* @return 濡傛灉鏄渶瑕佽繃婊ょ殑瀵硅薄锛屽垯杩斿洖true锛涘惁鍒欒繑鍥瀎alse銆�
*/
@SuppressWarnings("rawtypes")
- public boolean isFilterObject(final Object o)
- {
+ public boolean isFilterObject(final Object o) {
Class<?> clazz = o.getClass();
- if (clazz.isArray())
- {
+ if (clazz.isArray()) {
return clazz.getComponentType().isAssignableFrom(MultipartFile.class);
- }
- else if (Collection.class.isAssignableFrom(clazz))
- {
+ } else if (Collection.class.isAssignableFrom(clazz)) {
Collection collection = (Collection) o;
- for (Object value : collection) {
- return value instanceof MultipartFile;
- }
- }
- else if (Map.class.isAssignableFrom(clazz))
- {
+ for (Object value : collection) {
+ return value instanceof MultipartFile;
+ }
+ } else if (Map.class.isAssignableFrom(clazz)) {
Map map = (Map) o;
- for (Object value : map.entrySet()) {
- Map.Entry entry = (Map.Entry) value;
- return entry.getValue() instanceof MultipartFile;
- }
+ for (Object value : map.entrySet()) {
+ Map.Entry entry = (Map.Entry) value;
+ return entry.getValue() instanceof MultipartFile;
+ }
}
return o instanceof MultipartFile || o instanceof HttpServletRequest || o instanceof HttpServletResponse
|| o instanceof BindingResult;
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java
index 93502c0..3154fa2 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RateLimiterAspect.java
@@ -7,10 +7,8 @@
import com.ruoyi.common.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
-import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
-import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.redisson.api.RateType;
import org.springframework.stereotype.Component;
@@ -27,14 +25,8 @@
@Component
public class RateLimiterAspect {
- // 閰嶇疆缁囧叆鐐�
- @Pointcut("@annotation(com.ruoyi.common.annotation.RateLimiter)")
- public void rateLimiterPointCut() {
- }
-
- @Before("rateLimiterPointCut()")
- public void doBefore(JoinPoint point) throws Throwable {
- RateLimiter rateLimiter = getAnnotationRateLimiter(point);
+ @Before("@annotation(rateLimiter)")
+ public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable {
int time = rateLimiter.time();
int count = rateLimiter.count();
String combineKey = getCombineKey(rateLimiter, point);
@@ -53,20 +45,6 @@
} catch (Exception e) {
throw new RuntimeException("鏈嶅姟鍣ㄩ檺娴佸紓甯革紝璇风◢鍚庡啀璇�");
}
- }
-
- /**
- * 鏄惁瀛樺湪娉ㄨВ锛屽鏋滃瓨鍦ㄥ氨鑾峰彇
- */
- private RateLimiter getAnnotationRateLimiter(JoinPoint joinPoint) {
- Signature signature = joinPoint.getSignature();
- MethodSignature methodSignature = (MethodSignature) signature;
- Method method = methodSignature.getMethod();
-
- if (method != null) {
- return method.getAnnotation(RateLimiter.class);
- }
- return null;
}
public String getCombineKey(RateLimiter rateLimiter, JoinPoint point) {
diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
index b91b2e3..3814a2c 100644
--- a/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/aspectj/RepeatSubmitAspect.java
@@ -14,16 +14,12 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
-import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
-import org.aspectj.lang.annotation.Pointcut;
-import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
-import java.lang.reflect.Method;
/**
* 闃叉閲嶅鎻愪氦
@@ -40,14 +36,8 @@
private final RepeatSubmitProperties repeatSubmitProperties;
private final LockTemplate lockTemplate;
- // 閰嶇疆缁囧叆鐐�
- @Pointcut("@annotation(com.ruoyi.common.annotation.RepeatSubmit)")
- public void repeatSubmitPointCut() {
- }
-
- @Before("repeatSubmitPointCut()")
- public void doBefore(JoinPoint point) throws Throwable {
- RepeatSubmit repeatSubmit = getAnnotationRateLimiter(point);
+ @Before("@annotation(repeatSubmit)")
+ public void doBefore(JoinPoint point, RepeatSubmit repeatSubmit) throws Throwable {
// 濡傛灉娉ㄨВ涓嶄负0 鍒欎娇鐢ㄦ敞瑙f暟鍊�
long intervalTime = repeatSubmitProperties.getIntervalTime();
if (repeatSubmit.intervalTime() > 0) {
@@ -74,20 +64,6 @@
if (lock == null) {
throw new ServiceException("涓嶅厑璁搁噸澶嶆彁浜わ紝璇风◢鍚庡啀璇�!");
}
- }
-
- /**
- * 鏄惁瀛樺湪娉ㄨВ锛屽鏋滃瓨鍦ㄥ氨鑾峰彇
- */
- private RepeatSubmit getAnnotationRateLimiter(JoinPoint joinPoint) {
- Signature signature = joinPoint.getSignature();
- MethodSignature methodSignature = (MethodSignature) signature;
- Method method = methodSignature.getMethod();
-
- if (method != null) {
- return method.getAnnotation(RepeatSubmit.class);
- }
- return null;
}
}
--
Gitblit v1.9.3