love.keeping.starter.web.utils.SpelUtil Maven / Gradle / Ivy
The newest version!
package love.keeping.starter.web.utils;
import java.util.Map;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
/**
* Spel工具类
*
* @author Keeping
*/
public class SpelUtil {
private static final SpelExpressionParser PARSER;
static {
PARSER = new SpelExpressionParser();
}
public static Object parse(String expression, EvaluationContext ctx) {
return PARSER.parseExpression(expression).getValue(ctx);
}
public static Object parse(String expression, Map vars) {
EvaluationContext ctx = buildContext();
vars.forEach(ctx::setVariable);
return parse(expression, ctx);
}
public static EvaluationContext buildContext() {
return new StandardEvaluationContext();
}
}