码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 8.Spring EL与ExpressionParser


    Spring EL与ExpressionParser

    文章目录

    • Spring EL与ExpressionParser
      • 介绍
        • **使用SpEL来计算评估文字字符串表达式**
        • 使用SpEL来计算评估 bean 属性 – “item.name”

    介绍

    Spring表达式语言(SpEL)支持多种功能,并且可以测试这个特殊的“ExpressionParser”接口的表达式功能。

    下面是两个代码片段,展示了使用 Spring EL 的基本用法

    使用SpEL来计算评估文字字符串表达式

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

    使用SpEL来计算评估 bean 属性 – “item.name”

    Item item = new Item("yiibai", 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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    举几个例子来测试使用SpEL

    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("yiibai", 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 == 'yiibai'
    		Expression exp5 = parser.parseExpression("name == 'yiibai'");
    		boolean msg5 = exp5.getValue(itemContext, Boolean.class);
    		System.out.println(msg5);
    		
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    public class Item {
    
    	private String name;
    
    	private int qty;
    
    	public Item(String name, int qty) {
    		super();
    		this.name = name;
    		this.qty = qty;
    	}
    
    	//...
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    输出结果

    Hello World
    
    • 1
  • 相关阅读:
    计算机毕业设计 SSM老年公寓管理系统 养老院公寓管理系统 公寓住宿信息管理系统Java Vue MySQL数据库 远程调试 代码讲解
    【C语言数据结构】查找-顺序查找&折半查找
    【分布式websocket】聊天系统消息加密如何做
    VSCode连接远程服务器及docker
    Markdowm使用手册
    CNI设计解读
    leetcode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(中等)
    RK3588 rtc-hym8563设备开发
    计算机毕业设计 SSM+Vue房屋出租系统 房屋租赁合同信息管理系统 房屋租售管理系统Java Vue MySQL数据库 远程调试 代码讲解
    智慧校园-医务管理系统总体概述
  • 原文地址:https://blog.csdn.net/LuckFairyLuckBaby/article/details/132910173
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号