/user/profile
发布于

Springboot SPEL表达式

import lombok.Data;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

/**
 * @author: 我自己
 * @create: 2022-09-09 15:37
 * @description:
 */
@Data
public class T4 {
    static ExpressionParser parser = new SpelExpressionParser();
    static LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer();

    public static void main(String[] args) {

        EvaluationContext context = new StandardEvaluationContext();
        T4 t4 = new T4();
        t4.setUsername("1234");

        String spel = "#user.username";
        Expression expression = parser.parseExpression(spel);
        context.setVariable("user", t4);
        System.out.println(expression.getValue(context, Object.class));


        spel = "#flage>0.5";
        expression = parser.parseExpression(spel);
        context.setVariable("flage", 0.4);
        System.out.println(expression.getValue(context, Object.class));
    }

    private String username;

}
浏览 (107)
点赞 (1)
收藏
评论