Commit 27faa6153adece4fcca63a5f3a0cbeef500e1b28
1 parent
7f84e44e
优化spel表达式解析工具类
Showing
3 changed files
with
43 additions
and
7 deletions
src/main/java/cn/iocoder/dashboard/common/annotation/BizTracing.java
| @@ -13,7 +13,13 @@ import java.lang.annotation.*; | @@ -13,7 +13,13 @@ import java.lang.annotation.*; | ||
| 13 | */ | 13 | */ |
| 14 | public @interface BizTracing { | 14 | public @interface BizTracing { |
| 15 | 15 | ||
| 16 | + /** | ||
| 17 | + * 交易流水tag名 | ||
| 18 | + */ | ||
| 16 | String BIZ_ID_TAG = "bizId"; | 19 | String BIZ_ID_TAG = "bizId"; |
| 20 | + /** | ||
| 21 | + * 交易类型tag名 | ||
| 22 | + */ | ||
| 17 | String BIZ_TYPE_TAG = "bizType"; | 23 | String BIZ_TYPE_TAG = "bizType"; |
| 18 | 24 | ||
| 19 | String bizId(); | 25 | String bizId(); |
src/main/java/cn/iocoder/dashboard/common/annotation/BizTracingAop.java
| @@ -21,8 +21,8 @@ public class BizTracingAop { | @@ -21,8 +21,8 @@ public class BizTracingAop { | ||
| 21 | 21 | ||
| 22 | @Around(value = "@annotation(bizTracing)") | 22 | @Around(value = "@annotation(bizTracing)") |
| 23 | public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { | 23 | public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) { |
| 24 | - String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); | ||
| 25 | - String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); | 24 | + String bizId = (String) SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint); |
| 25 | + String bizType = (String) SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint); | ||
| 26 | if (StrUtil.isBlankIfStr(bizId)) { | 26 | if (StrUtil.isBlankIfStr(bizId)) { |
| 27 | log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType); | 27 | log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType); |
| 28 | return; | 28 | return; |
src/main/java/cn/iocoder/dashboard/util/sping/SpElUtil.java
| @@ -9,15 +9,31 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; | @@ -9,15 +9,31 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; | ||
| 9 | import org.springframework.expression.spel.support.StandardEvaluationContext; | 9 | import org.springframework.expression.spel.support.StandardEvaluationContext; |
| 10 | 10 | ||
| 11 | import java.lang.reflect.Method; | 11 | import java.lang.reflect.Method; |
| 12 | +import java.util.HashMap; | ||
| 13 | +import java.util.List; | ||
| 14 | +import java.util.Map; | ||
| 12 | 15 | ||
| 16 | +/** | ||
| 17 | + * SpEl解析类 | ||
| 18 | + * | ||
| 19 | + * @author mashu | ||
| 20 | + */ | ||
| 13 | public class SpElUtil { | 21 | public class SpElUtil { |
| 14 | 22 | ||
| 23 | + private static SpelExpressionParser parser = new SpelExpressionParser(); | ||
| 24 | + private static DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); | ||
| 25 | + | ||
| 15 | private SpElUtil() { | 26 | private SpElUtil() { |
| 16 | } | 27 | } |
| 17 | 28 | ||
| 18 | - public static String analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { | ||
| 19 | - SpelExpressionParser parser = new SpelExpressionParser(); | ||
| 20 | - DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer(); | 29 | + /** |
| 30 | + * 解析切面SpEL | ||
| 31 | + * | ||
| 32 | + * @param spElString 表达式 | ||
| 33 | + * @param joinPoint 切面点 | ||
| 34 | + * @return 执行界面 | ||
| 35 | + */ | ||
| 36 | + public static Object analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) { | ||
| 21 | // 通过joinPoint获取被注解方法 | 37 | // 通过joinPoint获取被注解方法 |
| 22 | MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); | 38 | MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); |
| 23 | Method method = methodSignature.getMethod(); | 39 | Method method = methodSignature.getMethod(); |
| @@ -33,8 +49,22 @@ public class SpElUtil { | @@ -33,8 +49,22 @@ public class SpElUtil { | ||
| 33 | for (int i = 0; i < args.length; i++) { | 49 | for (int i = 0; i < args.length; i++) { |
| 34 | context.setVariable(paramNames[i], args[i]); | 50 | context.setVariable(paramNames[i], args[i]); |
| 35 | } | 51 | } |
| 36 | - Object value = expression.getValue(context); | ||
| 37 | - return value == null ? "null" : value.toString(); | 52 | + return expression.getValue(context); |
| 53 | + } | ||
| 38 | 54 | ||
| 55 | + /** | ||
| 56 | + * 批量解析切面SpEL | ||
| 57 | + * | ||
| 58 | + * @param spElStrings 表达式 | ||
| 59 | + * @param joinPoint 切面点 | ||
| 60 | + * @return 执行界面 | ||
| 61 | + */ | ||
| 62 | + public static Map<String, Object> analysisSpEls(List<String> spElStrings, ProceedingJoinPoint joinPoint) { | ||
| 63 | + if (null == spElStrings) { | ||
| 64 | + return null; | ||
| 65 | + } | ||
| 66 | + Map<String, Object> resultMap = new HashMap<>(spElStrings.size()); | ||
| 67 | + spElStrings.forEach(expression -> resultMap.put(expression, analysisSpEl(expression, joinPoint))); | ||
| 68 | + return resultMap; | ||
| 39 | } | 69 | } |
| 40 | } | 70 | } |