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 
propertyExpression 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
11200
mkyong
true
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,092评论 19 139
  • 受伤幼儿病历 2016年7月29日下午4时,接到幼儿园老师焦老师电话,说孩子的手被划伤,伤情严重,正在第二人民医院...
    翔宇传媒阅读 4,310评论 0 0
  • Demo展示 上面是去加载网络图片,下面是加载本地图片 加载图片的几种方式 加载本地图片从项目中加载图片(一般是会...
    BlainPeng阅读 4,513评论 0 3
  • 最幸福的事莫过于,你爱的人也爱着你,你想念的人也想念着你。而我今天得到的不过是一句,哪有那么多话可说呢。这算不算最...
    阿狸的黑森林阅读 1,216评论 0 0

友情链接更多精彩内容