SpringElUtil 工具类

在项目中频繁使用SpringEL表达式进行数据解析,过滤等操作. 遂抽取工具类使用

Show me code


import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.expression.*;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeLocator;
import org.springframework.stereotype.Component;

/**
 * @author boommanpro
 * @date 2020/12/17 17:51
 */
@Component
public class SpringElUtil implements ApplicationContextAware {

    private static StandardEvaluationContext context;

    private static ExpressionParser expressionParser;

    private static final ConcurrentHashMap<String, Expression> EXPRESSION_MAP = new ConcurrentHashMap<>();

    public static <T> T getValue(String expressionString) {
        return getValue(expressionString, null);
    }

    @SuppressWarnings("all")
    public static <T> T getValue(String expressionString, Object rootObject) {
        Expression expression = getExpression(expressionString);
        return ((T) expression.getValue(context, rootObject));
    }


    private static Expression getExpression(String expressionString) {
        return EXPRESSION_MAP.computeIfAbsent(expressionString, s -> expressionParser.parseExpression(s));
    }


    @Override
    @SuppressWarnings("all")
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (applicationContext instanceof ConfigurableApplicationContext) {
            ConfigurableApplicationContext ac = (ConfigurableApplicationContext) applicationContext;
            context = new StandardEvaluationContext();
            context.addPropertyAccessor(new BeanExpressionContextAccessor());
            context.addPropertyAccessor(new BeanFactoryAccessor());
            context.addPropertyAccessor(new MapAccessor());
            context.addPropertyAccessor(new EnvironmentAccessor());
            context.setBeanResolver(new BeanFactoryResolver(ac.getBeanFactory()));
            context.setTypeLocator(new StandardTypeLocator(ac.getBeanFactory().getBeanClassLoader()));
            //初始化上下文
            expressionParser = new SpelExpressionParser();
        } else {
            throw new ApplicationContextException("can't cast ConfigurableApplicationContext");
        }
    }
}

Example

#result = 1 + 2
#{6.0221415E+23D instanceof T(Double)}
intArray[idx]-- le 0xFF
{1, 2, 3}
(true and false) || variable not null ? 1.0f : 0.0f
Members.?[Nationality == 'Serbian']
'5.00' matches '\^-?\\d+(\\.\\d{2})?$'
new java.lang.String('stringLiteral')
${myPropertyKey}
@myBean.instanceMethod(1, 2)
T(String).CASE_INSENSITIVE_ORDER