From 27faa6153adece4fcca63a5f3a0cbeef500e1b28 Mon Sep 17 00:00:00 2001 From: dark <189192663@qq.com> Date: Thu, 18 Mar 2021 23:12:49 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96spel=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F=E8=A7=A3=E6=9E=90=E5=B7=A5=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/annotation/BizTracing.java | 6 +++ .../common/annotation/BizTracingAop.java | 4 +- .../dashboard/util/sping/SpElUtil.java | 40 ++++++++++++++++--- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java index d254552d1..c974d8430 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java @@ -13,7 +13,13 @@ import java.lang.annotation.*; */ public @interface BizTracing { + /** + * 交易流水tag名 + */ String BIZ_ID_TAG = "bizId"; + /** + * 交易类型tag名 + */ String BIZ_TYPE_TAG = "bizType"; String bizId(); diff --git a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java index f8b2241b6..3f8b890d8 100644 --- a/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java +++ b/src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java @@ -21,8 +21,8 @@ public class BizTracingAop { @Around(value = "@annotation(bizTracing)") public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { - String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); - String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); + String bizId = (String) SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); + String bizType = (String) SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); if (StrUtil.isBlankIfStr(bizId)) { log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType); return; diff --git a/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java b/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java index 1291d326d..3c5a82a29 100644 --- a/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java +++ b/src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java @@ -9,15 +9,31 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +/** + * SpEl解析类 + * + * @author mashu + */ public class SpElUtil { + private static SpelExpressionParser parser = new SpelExpressionParser(); + private static DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); + private SpElUtil() { } - public static String analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { - SpelExpressionParser parser = new SpelExpressionParser(); - DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); + /** + * 解析切面SpEL + * + * @param spElString 表达式 + * @param joinPoint 切面点 + * @return 执行界面 + */ + public static Object analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { // 通过joinPoint获取被注解方法 MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); @@ -33,8 +49,22 @@ public class SpElUtil { for (int i = 0; i < args.length; i++) { context.setVariable(paramNames[i], args[i]); } - Object value = expression.getValue(context); - return value == null ? "null" : value.toString(); + return expression.getValue(context); + } + /** + * 批量解析切面SpEL + * + * @param spElStrings 表达式 + * @param joinPoint 切面点 + * @return 执行界面 + */ + public static Map analysisSpEls(List spElStrings, ProceedingJoinPoint joinPoint) { + if (null == spElStrings) { + return null; + } + Map resultMap = new HashMap<>(spElStrings.size()); + spElStrings.forEach(expression -> resultMap.put(expression, analysisSpEl(expression, joinPoint))); + return resultMap; } }