mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-01-19 11:40:05 +08:00
优化spel表达式解析工具类
This commit is contained in:
parent
7f84e44e94
commit
27faa6153a
@ -13,7 +13,13 @@ import java.lang.annotation.*;
|
|||||||
*/
|
*/
|
||||||
public @interface BizTracing {
|
public @interface BizTracing {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 交易流水tag名
|
||||||
|
*/
|
||||||
String BIZ_ID_TAG = "bizId";
|
String BIZ_ID_TAG = "bizId";
|
||||||
|
/**
|
||||||
|
* 交易类型tag名
|
||||||
|
*/
|
||||||
String BIZ_TYPE_TAG = "bizType";
|
String BIZ_TYPE_TAG = "bizType";
|
||||||
|
|
||||||
String bizId();
|
String bizId();
|
||||||
|
@ -21,8 +21,8 @@ public class BizTracingAop {
|
|||||||
|
|
||||||
@Around(value = "@annotation(bizTracing)")
|
@Around(value = "@annotation(bizTracing)")
|
||||||
public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) {
|
public void tagBizInfo(ProceedingJoinPoint joinPoint, BizTracing bizTracing) {
|
||||||
String bizId = SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint);
|
String bizId = (String) SpElUtil.analysisSpEl(bizTracing.bizId(), joinPoint);
|
||||||
String bizType = SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint);
|
String bizType = (String) SpElUtil.analysisSpEl(bizTracing.bizType(), joinPoint);
|
||||||
if (StrUtil.isBlankIfStr(bizId)) {
|
if (StrUtil.isBlankIfStr(bizId)) {
|
||||||
log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType);
|
log.error("empty biz: bizId[{}], bizType[{}].", bizId, bizType);
|
||||||
return;
|
return;
|
||||||
|
@ -9,15 +9,31 @@ import org.springframework.expression.spel.standard.SpelExpressionParser;
|
|||||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SpEl解析类
|
||||||
|
*
|
||||||
|
* @author mashu
|
||||||
|
*/
|
||||||
public class SpElUtil {
|
public class SpElUtil {
|
||||||
|
|
||||||
|
private static SpelExpressionParser parser = new SpelExpressionParser();
|
||||||
|
private static DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
|
||||||
|
|
||||||
private SpElUtil() {
|
private SpElUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) {
|
/**
|
||||||
SpelExpressionParser parser = new SpelExpressionParser();
|
* 解析切面SpEL
|
||||||
DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
|
*
|
||||||
|
* @param spElString 表达式
|
||||||
|
* @param joinPoint 切面点
|
||||||
|
* @return 执行界面
|
||||||
|
*/
|
||||||
|
public static Object analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) {
|
||||||
// 通过joinPoint获取被注解方法
|
// 通过joinPoint获取被注解方法
|
||||||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
||||||
Method method = methodSignature.getMethod();
|
Method method = methodSignature.getMethod();
|
||||||
@ -33,8 +49,22 @@ public class SpElUtil {
|
|||||||
for (int i = 0; i < args.length; i++) {
|
for (int i = 0; i < args.length; i++) {
|
||||||
context.setVariable(paramNames[i], args[i]);
|
context.setVariable(paramNames[i], args[i]);
|
||||||
}
|
}
|
||||||
Object value = expression.getValue(context);
|
return expression.getValue(context);
|
||||||
return value == null ? "null" : value.toString();
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 批量解析切面SpEL
|
||||||
|
*
|
||||||
|
* @param spElStrings 表达式
|
||||||
|
* @param joinPoint 切面点
|
||||||
|
* @return 执行界面
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> analysisSpEls(List<String> spElStrings, ProceedingJoinPoint joinPoint) {
|
||||||
|
if (null == spElStrings) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
Map<String, Object> resultMap = new HashMap<>(spElStrings.size());
|
||||||
|
spElStrings.forEach(expression -> resultMap.put(expression, analysisSpEl(expression, joinPoint)));
|
||||||
|
return resultMap;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user