spel表达式

package spring;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.SpelCompilerMode;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.*;

/**
 *  
 * @Date: 2022/1/19 13:41
 */
@Slf4j
public class Spel {
    public static void main(String[] args) {
        Spel spel = new Spel();
//        spel.cal();
        spel.demo1();
    }

    @Test
    public void cal() {
        String spelExpression = "#diffDead/#average * #score";
        Map<String, Object> variables = new HashMap<>();
        variables.put("diffDead", 25);
        variables.put("average", new Double(21));
        variables.put("score", 100);

        ExpressionParser parser = new SpelExpressionParser();
        StandardEvaluationContext context = new StandardEvaluationContext();
        context.setVariables(variables);
        Expression exp = parser.parseExpression(spelExpression);
        Integer value = exp.getValue(context, Integer.class);
        System.out.println("value:" + value);
    }

    @Test
    public void demo1() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression info = parser.parseExpression("'hello'");
        Object val = info.getValue();
        System.out.println(val);
    }

    @Test
    public void demo3() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression info = parser.parseExpression("'hello'.concat('1')");
        Object val = info.getValue();
        System.out.println(val);
    }

    @Test
    public void demo4() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression info = parser.parseExpression("'hello'.getBytes"); //the same as ‘hello’.bytes
        byte[] val = (byte[]) info.getValue();
        for (int i = 0; i < val.length; i++) {
            System.out.println(val[i]);
        }
    }

    @Test
    public void demo5() {
        ExpressionParser parser = new SpelExpressionParser();
        Expression info = parser.parseExpression("'hello'.getBytes.length");
        Object val = info.getValue();
        System.out.println(val);
    }

    @Test
    public void demo6() {
        // Create and set a calendar
        GregorianCalendar c = new GregorianCalendar();
        c.set(1856, 7, 9);
        // The constructor arguments are name, birthday, and nationality.
        Date time = c.getTime();
        Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");
        Inventor tesla2 = new Inventor("Nikola Tesla2", c.getTime(), "Serbian");
        ExpressionParser parser = new SpelExpressionParser();
        Expression exp = parser.parseExpression("name"); // Parse name as an expression
        String name = (String) exp.getValue(tesla);
        String name2 = (String) exp.getValue(tesla2);
        log.info(name);
        log.info(name2);
        exp = parser.parseExpression("name == 'Nikola Tesla'");
        boolean result = exp.getValue(tesla, Boolean.class);
        log.info("result:{}", result);
    }
    @Test
    public void demo7(){
        Simple simple = new Simple();
        simple.booleanList.add(true);
        simple.booleanList.add(true);
        //这个是做什么用的
        EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
        //两个参数,设置最大的size数
        SpelParserConfiguration config = new SpelParserConfiguration(true, true,4);
        ExpressionParser parser = new SpelExpressionParser(config);
        // "false" is passed in here as a String. SpEL and the conversion service
        // will recognize that it needs to be a Boolean and convert it accordingly.
        parser.parseExpression("booleanList[3]").setValue(context, simple, "false");
        // b is false
        simple.booleanList.forEach(r->{
            log.info("result:{}",r);
        });
    }
    @Test
    public void demo2() {
        SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,
                this.getClass().getClassLoader());

        SpelExpressionParser parser = new SpelExpressionParser(config);

        Expression expr = parser.parseExpression("payload");

        MyMessage message = new MyMessage();

        Object payload = expr.getValue(message);
    }

}

@Data
class MyMessage {
    String payload = "123";
}

@Data
class Inventor {
    String name;
    String location;
    Date birth;
    public Inventor(String name, Date birth, String location) {
        this.name = name;
        this.location = location;
        this.birth = birth;
    }
}
class Simple {
    public List<Boolean> booleanList = new ArrayList<Boolean>();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值