4cc45d73
dark
将 bizType 和 bizId...
|
1
2
3
4
5
6
7
8
9
10
11
|
package cn.iocoder.dashboard.util.sping;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import java.lang.reflect.Method;
|
27faa615
dark
优化spel表达式解析工具类
|
12
13
14
|
import java.util.HashMap;
import java.util.List;
import java.util.Map;
|
4cc45d73
dark
将 bizType 和 bizId...
|
15
|
|
27faa615
dark
优化spel表达式解析工具类
|
16
17
18
19
20
|
/**
* SpEl解析类
*
* @author mashu
*/
|
4cc45d73
dark
将 bizType 和 bizId...
|
21
22
|
public class SpElUtil {
|
27faa615
dark
优化spel表达式解析工具类
|
23
24
25
|
private static SpelExpressionParser parser = new SpelExpressionParser();
private static DefaultParameterNameDiscoverer nameDiscoverer = new DefaultParameterNameDiscoverer();
|
4cc45d73
dark
将 bizType 和 bizId...
|
26
27
28
|
private SpElUtil() {
}
|
27faa615
dark
优化spel表达式解析工具类
|
29
30
31
32
33
34
35
36
|
/**
* 解析切面SpEL
*
* @param spElString 表达式
* @param joinPoint 切面点
* @return 执行界面
*/
public static Object analysisSpEl(String spElString, ProceedingJoinPoint joinPoint) {
|
4cc45d73
dark
将 bizType 和 bizId...
|
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
// 通过joinPoint获取被注解方法
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
// 使用spring的DefaultParameterNameDiscoverer获取方法形参名数组
String[] paramNames = nameDiscoverer.getParameterNames(method);
// 解析过后的Spring表达式对象
Expression expression = parser.parseExpression(spElString);
// spring的表达式上下文对象
EvaluationContext context = new StandardEvaluationContext();
// 通过joinPoint获取被注解方法的形参
Object[] args = joinPoint.getArgs();
// 给上下文赋值
for (int i = 0; i < args.length; i++) {
context.setVariable(paramNames[i], args[i]);
}
|
27faa615
dark
优化spel表达式解析工具类
|
52
53
|
return expression.getValue(context);
}
|
4cc45d73
dark
将 bizType 和 bizId...
|
54
|
|
27faa615
dark
优化spel表达式解析工具类
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
/**
* 批量解析切面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;
|
4cc45d73
dark
将 bizType 和 bizId...
|
69
70
|
}
}
|