spring el表达式计算代码样例

package el;

import java.math.BigDecimal;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

public class Test {
    public static void main(String[] args) {

        long currentTimeMillis = System.currentTimeMillis();
        ExpressionParser parser = new SpelExpressionParser();
//        Expression parseExpression = parser.parseExpression("#person.getString() == '1' && #person.isBool() && #person.integer <= 18357 && #person.doub == 2.010 && #person.bigDecimal >= 20.1");
        Expression parseExpression = parser.parseExpression("(#person.getString() == '1' || #person.getString() == '2') && #person.isBool()");
        for (int i = 0; i < 10000; i++) {
            Person p = new Person(18357, "1", new BigDecimal("20.1"), true, 2.010d);
            StandardEvaluationContext conext = new StandardEvaluationContext();
            conext.setVariable("person", p);
//            Expression parseExpression = parser.parseExpression("#person.bool");
//            Expression parseExpression = parser.parseExpression("#person.isBool()");
//            Expression parseExpression = parser.parseExpression("#person.getString().equals('2') ");
//            Expression parseExpression = parser.parseExpression("#person.integer == 18357 ");
//            Expression parseExpression = parser.parseExpression("#person.doub == 2.010 ");
//            Expression parseExpression = parser.parseExpression("#person.bigDecimal > 20.1 ");
            Boolean value = parseExpression.getValue(conext, Boolean.class);
            System.out.println(value);
        }
        System.out.println(System.currentTimeMillis() - currentTimeMillis);
    }
}
package el;

import java.math.BigDecimal;

public class Person {

    private Integer integer;
    private String string;
    private BigDecimal bigDecimal;
    private Boolean bool;
    private Double doub;

    public Person(Integer integer, String string, BigDecimal bigDecimal, Boolean bool, Double doub) {
        super();
        this.integer = integer;
        this.string = string;
        this.bigDecimal = bigDecimal;
        this.bool = bool;
        this.doub = doub;
    }

    public Integer getInteger() {
        return integer;
    }

    public String getString() {
        return string;
    }

    public BigDecimal getBigDecimal() {
        return bigDecimal;
    }

    public Boolean isBool() {
        return bool;
    }

    public Double getDoub() {
        return doub;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值