🚚 SpELUtil.java rename to SpelUtil.java, and move to ballcat-common-util
This commit is contained in:
@@ -60,5 +60,11 @@
|
||||
<artifactId>jsoup</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.hccake.ballcat.common.redis.core;
|
||||
package com.hccake.ballcat.common.util;
|
||||
|
||||
import org.springframework.context.expression.MethodBasedEvaluationContext;
|
||||
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||
@@ -14,7 +14,11 @@ import java.lang.reflect.Method;
|
||||
* @version 1.0
|
||||
* @date 2019/9/3 10:29
|
||||
*/
|
||||
public class SpELUtil {
|
||||
@SuppressWarnings("SpellCheckingInspection")
|
||||
public final class SpelUtils {
|
||||
|
||||
private SpelUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* SpEL 解析器
|
||||
@@ -22,48 +26,53 @@ public class SpELUtil {
|
||||
public static final ExpressionParser PARSER = new SpelExpressionParser();
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* 方法参数获取
|
||||
*/
|
||||
public static final ParameterNameDiscoverer PARAMETER_NAME_DISCOVERER = new LocalVariableTableParameterNameDiscoverer();
|
||||
|
||||
/**
|
||||
* 支持 #p0 参数索引的表达式解析
|
||||
* @param rootObject 根对象,method 所在的对象
|
||||
* @param spEL 表达式
|
||||
* @param rootObject 根对象, method 所在类的对象实例
|
||||
* @param spelExpression spel 表达式
|
||||
* @param method 目标方法
|
||||
* @param args 方法入参
|
||||
* @return 解析后的字符串
|
||||
*/
|
||||
public static String parseValueToString(Object rootObject, Method method, Object[] args, String spEL) {
|
||||
|
||||
StandardEvaluationContext context = getSpElContext(rootObject, method, args);
|
||||
return parseValueToString(context, spEL);
|
||||
public static String parseValueToString(Object rootObject, Method method, Object[] args, String spelExpression) {
|
||||
StandardEvaluationContext context = getSpelContext(rootObject, method, args);
|
||||
return parseValueToString(context, spelExpression);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支持 #p0 参数索引的表达式解析
|
||||
* @param rootObject 根对象, method 所在的对象
|
||||
* @param method ,目标方法
|
||||
* @param args 方法入参
|
||||
* @return 解析后的字符串
|
||||
* @param method 目标方法
|
||||
* @param args 方法实际入参
|
||||
* @return StandardEvaluationContext spel 上下文
|
||||
*/
|
||||
public static StandardEvaluationContext getSpElContext(Object rootObject, Method method, Object[] args) {
|
||||
|
||||
String[] paraNameArr = PARAMETER_NAME_DISCOVERER.getParameterNames(method);
|
||||
// SPEL 上下文
|
||||
public static StandardEvaluationContext getSpelContext(Object rootObject, Method method, Object[] args) {
|
||||
// spel 上下文
|
||||
StandardEvaluationContext context = new MethodBasedEvaluationContext(rootObject, method, args,
|
||||
PARAMETER_NAME_DISCOVERER);
|
||||
// 把方法参数放入 SPEL 上下文中
|
||||
for (int i = 0; i < paraNameArr.length; i++) {
|
||||
context.setVariable(paraNameArr[i], args[i]);
|
||||
// 方法参数名数组
|
||||
String[] parameterNames = PARAMETER_NAME_DISCOVERER.getParameterNames(method);
|
||||
// 把方法参数放入 spel 上下文中
|
||||
if (parameterNames != null && parameterNames.length > 0) {
|
||||
for (int i = 0; i < parameterNames.length; i++) {
|
||||
context.setVariable(parameterNames[i], args[i]);
|
||||
}
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
public static String parseValueToString(StandardEvaluationContext context, String spEL) {
|
||||
return PARSER.parseExpression(spEL).getValue(context, String.class);
|
||||
/**
|
||||
* 解析 spel 表达式
|
||||
* @param context spel 上下文
|
||||
* @param spelExpression spel 表达式
|
||||
* @return String 解析后的字符串
|
||||
*/
|
||||
public static String parseValueToString(StandardEvaluationContext context, String spelExpression) {
|
||||
return PARSER.parseExpression(spelExpression).getValue(context, String.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +1,13 @@
|
||||
package com.hccake.ballcat.common.redis.core;
|
||||
|
||||
import com.hccake.ballcat.common.redis.config.CachePropertiesHolder;
|
||||
import com.hccake.ballcat.common.util.SpelUtils;
|
||||
import org.springframework.expression.spel.support.StandardEvaluationContext;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -21,89 +20,50 @@ public class KeyGenerator {
|
||||
/**
|
||||
* SpEL 上下文
|
||||
*/
|
||||
StandardEvaluationContext spElContext;
|
||||
StandardEvaluationContext spelContext;
|
||||
|
||||
public KeyGenerator(Object target, Method method, Object[] arguments) {
|
||||
this.spElContext = SpELUtil.getSpElContext(target, method, arguments);
|
||||
this.spelContext = SpelUtils.getSpelContext(target, method, arguments);
|
||||
}
|
||||
|
||||
public String getKey(String key, String spELExpressions) {
|
||||
/**
|
||||
* 根据 keyPrefix 和 keyJoint 获取完整的 key 信息
|
||||
* @param keyPrefix key 前缀
|
||||
* @param keyJoint key 拼接元素,值为 spel 表达式,可为空
|
||||
* @return 拼接完成的 key
|
||||
*/
|
||||
public String getKey(String keyPrefix, String keyJoint) {
|
||||
// 根据 keyJoint 判断是否需要拼接
|
||||
if (spELExpressions == null || spELExpressions.length() == 0) {
|
||||
return key;
|
||||
if (keyJoint == null || keyJoint.length() == 0) {
|
||||
return keyPrefix;
|
||||
}
|
||||
|
||||
// 获取所有需要拼接的元素, 组装进集合中
|
||||
String joint = SpELUtil.parseValueToString(spElContext, spELExpressions);
|
||||
String joint = SpelUtils.parseValueToString(spelContext, keyJoint);
|
||||
Assert.notNull(joint, "Key joint cannot be null!");
|
||||
|
||||
if (!StringUtils.hasText(key)) {
|
||||
if (!StringUtils.hasText(keyPrefix)) {
|
||||
return joint;
|
||||
}
|
||||
// 拼接后返回
|
||||
return jointKey(key, joint);
|
||||
}
|
||||
|
||||
public List<String> getKeys(String key, String keyJoint, Collection<String> multiByItem) {
|
||||
String keyPrefix = getKey(key, keyJoint);
|
||||
|
||||
List<String> list = new ArrayList<>();
|
||||
for (String item : multiByItem) {
|
||||
list.add(jointKey(keyPrefix, item));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param key
|
||||
* @param spELExpressions
|
||||
* @return
|
||||
*/
|
||||
public String getKeys(String key, String[] spELExpressions) {
|
||||
// 根据keyJoint 判断是否需要拼接
|
||||
if (spELExpressions == null || spELExpressions.length == 0) {
|
||||
return key;
|
||||
}
|
||||
|
||||
// 获取所有需要拼接的元素, 组装进集合中
|
||||
List<String> list = new ArrayList<>(spELExpressions.length + 1);
|
||||
list.add(key);
|
||||
for (String joint : spELExpressions) {
|
||||
String s = parseSpEL(joint);
|
||||
Assert.notNull(s, "Key joint cannot be null!");
|
||||
list.add(s);
|
||||
}
|
||||
|
||||
// 拼接后返回
|
||||
return jointKey(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析SPEL
|
||||
* @param field
|
||||
* @return
|
||||
*/
|
||||
public String parseSpEL(String field) {
|
||||
return SpELUtil.parseValueToString(spElContext, field);
|
||||
return jointKey(keyPrefix, joint);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接key, 默认使用 :作为分隔符
|
||||
* @param list
|
||||
* @return
|
||||
* @param keyItems 用于拼接 key 的元素列表
|
||||
* @return 拼接完成的 key
|
||||
*/
|
||||
public String jointKey(List<String> list) {
|
||||
return String.join(CachePropertiesHolder.delimiter(), list);
|
||||
public String jointKey(List<String> keyItems) {
|
||||
return String.join(CachePropertiesHolder.delimiter(), keyItems);
|
||||
}
|
||||
|
||||
/**
|
||||
* 拼接key, 默认使用 :作为分隔符
|
||||
* @param items
|
||||
* @return
|
||||
* @param keyItems 用于拼接 key 的元素列表
|
||||
* @return 拼接完成的 key
|
||||
*/
|
||||
public String jointKey(String... items) {
|
||||
return jointKey(Arrays.asList(items));
|
||||
public String jointKey(String... keyItems) {
|
||||
return jointKey(Arrays.asList(keyItems));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user