zxl
5 天以前 8063ee7eee51bfe25a09428e6efc60f828b270c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package cn.lili.common.utils;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
 
/**
 * SpelUtil
 *
 * @author Chopper
 * @version v1.0
 * 2021-01-11 10:45
 */
public class SpelUtil {
 
 
    /**
     * spel表达式解析器
     */
    private static SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
    /**
     * 参数名发现器
     */
    private static DefaultParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
 
    /**
     * 转换 jspl参数
     *
     * @param joinPoint
     * @param spel
     * @return
     */
    public static String compileParams(JoinPoint joinPoint, String spel) { //Spel表达式解析日志信息
        //获得方法参数名数组
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
 
        String[] parameterNames = parameterNameDiscoverer.getParameterNames(signature.getMethod());
        if (parameterNames != null && parameterNames.length > 0) {
            EvaluationContext context = new StandardEvaluationContext();
 
            //获取方法参数值
            Object[] args = joinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                //替换spel里的变量值为实际值, 比如 #user -->  user对象
                context.setVariable(parameterNames[i], args[i]);
            }
            return spelExpressionParser.parseExpression(spel).getValue(context).toString();
        }
        return "";
    }
 
    /**
     * 转换 jspl参数
     *
     * @param joinPoint
     * @param spel
     * @return
     */
    public static String compileParams(JoinPoint joinPoint, Object rvt, String spel) { //Spel表达式解析日志信息
        //获得方法参数名数组
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
 
        String[] parameterNames = parameterNameDiscoverer.getParameterNames(signature.getMethod());
        if (parameterNames != null && parameterNames.length > 0) {
            EvaluationContext context = new StandardEvaluationContext();
 
            //获取方法参数值
            Object[] args = joinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                //替换spel里的变量值为实际值, 比如 #user -->  user对象
                context.setVariable(parameterNames[i], args[i]);
            }
            context.setVariable("rvt", rvt);
            return spelExpressionParser.parseExpression(spel).getValue(context).toString();
        }
        return "";
    }
}