zoukankan      html  css  js  c++  java
  • Test Spring el with ExpressionParser

    Spring expression language (SpEL) supports many functionality, and you can test those expression features with this special “ExpressionParser” interface.

    Here’s two code snippets, show the basic usage of using Spring EL.

    SpEL to evaluate the literal string expression.

    ExpressionParser parser = new SpelExpressionParser();
    Expression exp = parser.parseExpression("'put spel expression here'");
    String msg = exp.getValue(String.class);
    

    SpEL to evaluate the bean property – “item.name”.

    Item item = new Item("mkyong", 100);
    StandardEvaluationContext itemContext = new StandardEvaluationContext(item);
    		
    //display the value of item.name property
    Expression exp = parser.parseExpression("name");
    String msg = exp.getValue(itemContext, String.class);
    

    Few examples to test SpEL. The codes and comments should be self-exploratory.

    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 App {
    	public static void main(String[] args) {
    		
    		ExpressionParser parser = new SpelExpressionParser();
    		
    		//literal expressions 
    		Expression exp = parser.parseExpression("'Hello World'");
    		String msg1 = exp.getValue(String.class);
    		System.out.println(msg1);
    		
    		//method invocation
    		Expression exp2 = parser.parseExpression("'Hello World'.length()");  
    		int msg2 = (Integer) exp2.getValue();
    		System.out.println(msg2);
    		
    		//Mathematical operators
    		Expression exp3 = parser.parseExpression("100 * 2");  
    		int msg3 = (Integer) exp3.getValue();
    		System.out.println(msg3);
    		
    		//create an item object
    		Item item = new Item("mkyong", 100);
    		//test EL with item object
    		StandardEvaluationContext itemContext = new StandardEvaluationContext(item);
    		
    		//display the value of item.name property
    		Expression exp4 = parser.parseExpression("name");
    		String msg4 = exp4.getValue(itemContext, String.class);
    		System.out.println(msg4);
    		
    		//test if item.name == 'mkyong'
    		Expression exp5 = parser.parseExpression("name == 'mkyong'");
    		boolean msg5 = exp5.getValue(itemContext, Boolean.class);
    		System.out.println(msg5);
    		
    	}
    }
    
    public class Item {
    
    	private String name;
    
    	private int qty;
    
    	public Item(String name, int qty) {
    		super();
    		this.name = name;
    		this.qty = qty;
    	}
    
    	//...
    }
    

    Output

    Hello World
    11
    200
    mkyong
    true
    
  • 相关阅读:
    视频列表页面滑动时停止视频播放
    小程序跳转到另外一个小程序的设置
    小程序悬浮框
    wx.previewimage预览返回会触发onshow的处理方法
    uni-app小程序滑动事件
    小程序转uni-app用到的一些方法
    第四阶段:DRF day82 DRF--DRF中三大认证中的jwt和频率模块解析
    第四阶段:DRF day81 DRF--DRF中三大认证中认证模块和权限模块详解
    第四阶段:DRF day80 DRF--DRF中的视图家族及工具视图类
    第四阶段:DRF day79 DRF--DRF中通过ModelSerializer实现单查单增群查群改等操作
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4749788.html
Copyright © 2011-2022 走看看