提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.common.util.spring; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
|
4 |
import cn.hutool.core.map.MapUtil; |
|
5 |
import cn.hutool.core.util.ArrayUtil; |
|
6 |
import org.aspectj.lang.JoinPoint; |
|
7 |
import org.aspectj.lang.reflect.MethodSignature; |
|
8 |
import org.springframework.core.DefaultParameterNameDiscoverer; |
|
9 |
import org.springframework.core.ParameterNameDiscoverer; |
|
10 |
import org.springframework.expression.EvaluationContext; |
|
11 |
import org.springframework.expression.ExpressionParser; |
|
12 |
import org.springframework.expression.spel.standard.SpelExpressionParser; |
|
13 |
import org.springframework.expression.spel.support.StandardEvaluationContext; |
|
14 |
|
|
15 |
import java.lang.reflect.Method; |
|
16 |
import java.util.Collections; |
|
17 |
import java.util.List; |
|
18 |
import java.util.Map; |
|
19 |
|
|
20 |
/** |
|
21 |
* Spring EL 表达式的工具类 |
|
22 |
* |
|
23 |
* @author mashu |
|
24 |
*/ |
|
25 |
public class SpringExpressionUtils { |
|
26 |
|
|
27 |
/** |
|
28 |
* Spring EL 表达式解析器 |
|
29 |
*/ |
|
30 |
private static final ExpressionParser EXPRESSION_PARSER = new SpelExpressionParser(); |
|
31 |
/** |
|
32 |
* 参数名发现器 |
|
33 |
*/ |
|
34 |
private static final ParameterNameDiscoverer PARAMETER_NAME_DISCOVERER = new DefaultParameterNameDiscoverer(); |
|
35 |
|
|
36 |
private SpringExpressionUtils() { |
|
37 |
} |
|
38 |
|
|
39 |
/** |
|
40 |
* 从切面中,单个解析 EL 表达式的结果 |
|
41 |
* |
|
42 |
* @param joinPoint 切面点 |
|
43 |
* @param expressionString EL 表达式数组 |
|
44 |
* @return 执行界面 |
|
45 |
*/ |
|
46 |
public static Object parseExpression(JoinPoint joinPoint, String expressionString) { |
|
47 |
Map<String, Object> result = parseExpressions(joinPoint, Collections.singletonList(expressionString)); |
|
48 |
return result.get(expressionString); |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* 从切面中,批量解析 EL 表达式的结果 |
|
53 |
* |
|
54 |
* @param joinPoint 切面点 |
|
55 |
* @param expressionStrings EL 表达式数组 |
|
56 |
* @return 结果,key 为表达式,value 为对应值 |
|
57 |
*/ |
|
58 |
public static Map<String, Object> parseExpressions(JoinPoint joinPoint, List<String> expressionStrings) { |
|
59 |
// 如果为空,则不进行解析 |
|
60 |
if (CollUtil.isEmpty(expressionStrings)) { |
|
61 |
return MapUtil.newHashMap(); |
|
62 |
} |
|
63 |
|
|
64 |
// 第一步,构建解析的上下文 EvaluationContext |
|
65 |
// 通过 joinPoint 获取被注解方法 |
|
66 |
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); |
|
67 |
Method method = methodSignature.getMethod(); |
|
68 |
// 使用 spring 的 ParameterNameDiscoverer 获取方法形参名数组 |
|
69 |
String[] paramNames = PARAMETER_NAME_DISCOVERER.getParameterNames(method); |
|
70 |
// Spring 的表达式上下文对象 |
|
71 |
EvaluationContext context = new StandardEvaluationContext(); |
|
72 |
// 给上下文赋值 |
|
73 |
if (ArrayUtil.isNotEmpty(paramNames)) { |
|
74 |
Object[] args = joinPoint.getArgs(); |
|
75 |
for (int i = 0; i < paramNames.length; i++) { |
|
76 |
context.setVariable(paramNames[i], args[i]); |
|
77 |
} |
|
78 |
} |
|
79 |
|
|
80 |
// 第二步,逐个参数解析 |
|
81 |
Map<String, Object> result = MapUtil.newHashMap(expressionStrings.size(), true); |
|
82 |
expressionStrings.forEach(key -> { |
|
83 |
Object value = EXPRESSION_PARSER.parseExpression(key).getValue(context); |
|
84 |
result.put(key, value); |
|
85 |
}); |
|
86 |
return result; |
|
87 |
} |
|
88 |
|
|
89 |
} |