一)什么是SpEL
SpEL -- Spring Expression Language. Spring的表达式语言。举个最简单的例子:

Java代码
  1. ExpressionParser parser =new SpelExpressionParser();
  2. Expression exp = parser.parseExpression("'Hello World'");
  3. String message = (String) exp.getValue();

ExpressionParser parser =new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'");
String message = (String) exp.getValue();

最后 message的值就是 Hello World, 表达式中的单引号''就是表达String类型的一种格式。另外值得注意的一点时,当表达式不符合规范时, parser.parseExpression语句会抛出ParseException;而exp.getValue会抛出EvaluationException
而为了避免最后的类型转换,我们还可以这样写:

Java代码
  1. String message = exp.getValue(String.class);

String message = exp.getValue(String.class);

二)SpEL举例中需要使用到的类
以下是本文介绍SpEL过程中需要使用到的类,在此列出,以供后续介绍中使用参考:
Inventor.java

Java代码
  1. package org.spring.samples.spel.inventor;
  2. import java.util.Date;
  3. import java.util.GregorianCalendar;
  4. public class Inventor {
  5. private String name;
  6. private String nationality;
  7. private String[] inventions;
  8. private Date birthdate;
  9. private PlaceOfBirth placeOfBirth;
  10. public Inventor(String name, String nationality){
  11. GregorianCalendar c= new GregorianCalendar();
  12. this.name = name;
  13. this.nationality = nationality;
  14. this.birthdate = c.getTime();
  15. }
  16. public Inventor(String name, Date birthdate, String nationality) {
  17. this.name = name;
  18. this.nationality = nationality;
  19. this.birthdate = birthdate;
  20. }
  21. public Inventor() {}
  22. public String getName() { return name;}
  23. public void setName(String name) { this.name = name;}
  24. public String getNationality() { return nationality;}
  25. public void setNationality(String nationality) { this.nationality = nationality; }
  26. public Date getBirthdate() { return birthdate;}
  27. public void setBirthdate(Date birthdate) { this.birthdate = birthdate;}
  28. public PlaceOfBirth getPlaceOfBirth() { return placeOfBirth;}
  29. public void setPlaceOfBirth(PlaceOfBirth placeOfBirth) { this.placeOfBirth = placeOfBirth;}
  30. public void setInventions(String[] inventions) { this.inventions = inventions; }
  31. public String[] getInventions() { return inventions;}
  32. }

package org.spring.samples.spel.inventor;
import java.util.Date;
import java.util.GregorianCalendar;public class Inventor {private String name;private String nationality;private String[] inventions;private Date birthdate;private PlaceOfBirth placeOfBirth;public Inventor(String name, String nationality){GregorianCalendar c= new GregorianCalendar();this.name = name;this.nationality = nationality;this.birthdate = c.getTime();}public Inventor(String name, Date birthdate, String nationality) {this.name = name;this.nationality = nationality;this.birthdate = birthdate;}public Inventor() {}public String getName() { return name;}public void setName(String name) { this.name = name;}public String getNationality() { return nationality;}public void setNationality(String nationality) { this.nationality = nationality; }public Date getBirthdate() { return birthdate;}public void setBirthdate(Date birthdate) { this.birthdate = birthdate;}public PlaceOfBirth getPlaceOfBirth() { return placeOfBirth;}public void setPlaceOfBirth(PlaceOfBirth placeOfBirth) { this.placeOfBirth = placeOfBirth;}public void setInventions(String[] inventions) { this.inventions = inventions; }public String[] getInventions() { return inventions;}
}

PlaceOfBirth.java

Java代码
  1. package org.spring.samples.spel.inventor;
  2. public class PlaceOfBirth {
  3. private String city;
  4. private String country;
  5. public PlaceOfBirth(String city) {this.city=city;}
  6. public PlaceOfBirth(String city, String country){
  7. this(city);
  8. this.country = country;
  9. }
  10. public String getCity() {return city;}
  11. public void setCity(String s) {this.city = s;}
  12. public String getCountry() {return country;}
  13. public void setCountry(String country) {this.country = country;}
  14. }

package org.spring.samples.spel.inventor;public class PlaceOfBirth {private String city;private String country;public PlaceOfBirth(String city) {this.city=city;}public PlaceOfBirth(String city, String country){this(city);this.country = country;}public String getCity() {return city;}public void setCity(String s) {this.city = s;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}
}

Society.java

Java代码
  1. package org.spring.samples.spel.inventor;
  2. import java.util.*;
  3. public class Society {
  4. private String name;
  5. public static String Advisors = "advisors";
  6. public static String President = "president";
  7. private List<Inventor> members = new ArrayList<Inventor>();
  8. private Map officers = new HashMap();
  9. public List getMembers() {return members;}
  10. public Map getOfficers() {return officers;}
  11. public String getName() {return name;}
  12. public void setName(String name) {this.name = name;}
  13. public boolean isMember(String name){
  14. boolean found = false;
  15. for (Inventor inventor : members) {
  16. if (inventor.getName().equals(name)){
  17. found = true;
  18. break;
  19. }
  20. }
  21. return found;
  22. }
  23. }

package org.spring.samples.spel.inventor;
import java.util.*;public class Society {private String name;public static String Advisors = "advisors";public static String President = "president";private List<Inventor> members = new ArrayList<Inventor>();private Map officers = new HashMap();public List getMembers() {return members;}public Map getOfficers() {return officers;}public String getName() {return name;}public void setName(String name) {this.name = name;}public boolean isMember(String name){boolean found = false;for (Inventor inventor : members) {if (inventor.getName().equals(name)){found = true;break;}}return found;}
}

转载于:https://blog.51cto.com/daheyuan/1139361

Spring --- SpEL相关推荐

  1. 5、Spring SPEL使用之--在XML中使用SPEL

    SPEL(Spring Expression Language)即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂表达式 ...

  2. Spring SpEL表达式

    spring expression language 是在spring3.0以后的版本提供 它类似于ognl或el表达式,它可以提供在程序运行时构造复杂表达式来完成对象属性存储及方法调用等. Spel ...

  3. Spring SpEL表达式的使用

    文章目录 一.SpEL介绍 二.SpEL用法 1. 在@Value注解中使用 2. 在XML配置中使用 3. 在代码中创建Expression对象 三.SpEL原理 1. 解析器:Expression ...

  4. java spel_Java spring SPEL表达式注入

    首先搭建一个Spring环境. 文章可以看https://blog.csdn.net/cflys/article/details/70598903 一步步跟下去就行. 在src目录添加spring-c ...

  5. spring spel 获取环境变量

    除了访问<util:properties>所生命的集合中的属性,Spring还为SpEL选择了两种特殊的选择属性的方式:systemEnvironment和systemProperties ...

  6. Spring SpEL 的使用

    链接: 如何优雅记录日志. 链接: SpEL你感兴趣的实现原理浅析spring-expression. 链接: spEL-基础语法+注解中动态调用Bean方法. Spring EL Spring 3 ...

  7. Spring学习-- SpEL表达式

    转载自 https://www.cnblogs.com/goodcheap/p/6490896.html 目录 Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式 ...

  8. Spring之SpEL

    前言 Spring3引入了Spring表达式语言(SpEL),可以像jsp中的el表达式一样,在运行期动态执行,从而得到需要的值. SpEL的特性包括: - 使用Bean的ID来引用Bean - 调用 ...

  9. spring基础知识 (10): SpEL表达式

    什么是SpEL Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于 EL表达式:SpEL 使用 #{-}作为定界符,所有在大框号中的字符都将被认 ...

最新文章

  1. Go 学习笔记(39)— Go 反射
  2. 动态内存管理和智能指针 2.0 -- shared_ptr
  3. GitHub 标星 20000+,国产 AI 开源从算法开始突破 | 专访商汤联合创始人林达华
  4. exchange online share mailbox senditem
  5. quot;streambufquot; ambiguous symbol的问题如何解决
  6. JZOJ 5483. 【清华集训2017模拟11.26】简单路径
  7. (课程学习笔记)玩转Linux:常用命令实例指南
  8. linux软件安装非系统盘,linux操作系统可不可以像安装windows软件一样在windows系统下的硬盘上安装...
  9. php 变量文件间传递,同一文件的两个JS函数之间如何传变量?
  10. drf版本控制 和django缓存,跨域问题,
  11. 过滤器做权限校验以及遇到的坑
  12. Maven静态资源导出失败问题
  13. 博弈论 | 演化博弈理论(Evolutionary Game Theory)的理解
  14. matlab怎么导入数据格式,Matlab导入Excel文件中的数据的详细教程分享
  15. [BZOJ1233][Usaco2009Open]干草堆tower(单调队列优化)
  16. 北京图灵学院 Python(02)
  17. 【论文阅读笔记】BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
  18. 移远BC35-G模组通过LWM2M协议接入OneNet教程
  19. (附源码)计算机毕业设计SSM基于web的网上订餐系统
  20. 易语言认识易语言数据类型

热门文章

  1. 使用Embedded VC++开发通讯终端及代码分析
  2. 重拾-Spring Transaction
  3. (原創) 如何解決移除DSP Builder後,在Matlab殘留錯誤訊息的問題? (SOC) (DSP Builder) (Matlab)...
  4. ECMAScript 6 Features 中文版
  5. 体验是情感的(译稿)
  6. 【译】Asp.net MVC 利用自定义RouteHandler来防止图片盗链 (转)
  7. 特斯拉致命车祸最新调查结果发布
  8. Zabbix监控MongoDB
  9. jsp常见获取地址函数之间的不同
  10. Selenium Web 自动化 - Selenium常用API