Spring --- SpEL
一)什么是SpEL
SpEL -- Spring Expression Language. Spring的表达式语言。举个最简单的例子:
- ExpressionParser parser =new SpelExpressionParser();
- Expression exp = parser.parseExpression("'Hello World'");
- 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
而为了避免最后的类型转换,我们还可以这样写:
- String message = exp.getValue(String.class);
String message = exp.getValue(String.class);
二)SpEL举例中需要使用到的类
以下是本文介绍SpEL过程中需要使用到的类,在此列出,以供后续介绍中使用参考:
Inventor.java
- 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;}
- }
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
- 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;}
- }
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
- 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;
- }
- }
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相关推荐
- 5、Spring SPEL使用之--在XML中使用SPEL
SPEL(Spring Expression Language)即Spring3中功能丰富强大的表达式语言,简称SpEL.SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂表达式 ...
- Spring SpEL表达式
spring expression language 是在spring3.0以后的版本提供 它类似于ognl或el表达式,它可以提供在程序运行时构造复杂表达式来完成对象属性存储及方法调用等. Spel ...
- Spring SpEL表达式的使用
文章目录 一.SpEL介绍 二.SpEL用法 1. 在@Value注解中使用 2. 在XML配置中使用 3. 在代码中创建Expression对象 三.SpEL原理 1. 解析器:Expression ...
- java spel_Java spring SPEL表达式注入
首先搭建一个Spring环境. 文章可以看https://blog.csdn.net/cflys/article/details/70598903 一步步跟下去就行. 在src目录添加spring-c ...
- spring spel 获取环境变量
除了访问<util:properties>所生命的集合中的属性,Spring还为SpEL选择了两种特殊的选择属性的方式:systemEnvironment和systemProperties ...
- Spring SpEL 的使用
链接: 如何优雅记录日志. 链接: SpEL你感兴趣的实现原理浅析spring-expression. 链接: spEL-基础语法+注解中动态调用Bean方法. Spring EL Spring 3 ...
- Spring学习-- SpEL表达式
转载自 https://www.cnblogs.com/goodcheap/p/6490896.html 目录 Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式 ...
- Spring之SpEL
前言 Spring3引入了Spring表达式语言(SpEL),可以像jsp中的el表达式一样,在运行期动态执行,从而得到需要的值. SpEL的特性包括: - 使用Bean的ID来引用Bean - 调用 ...
- spring基础知识 (10): SpEL表达式
什么是SpEL Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. 语法类似于 EL表达式:SpEL 使用 #{-}作为定界符,所有在大框号中的字符都将被认 ...
最新文章
- Go 学习笔记(39)— Go 反射
- 动态内存管理和智能指针 2.0 -- shared_ptr
- GitHub 标星 20000+,国产 AI 开源从算法开始突破 | 专访商汤联合创始人林达华
- exchange online share mailbox senditem
- quot;streambufquot; ambiguous symbol的问题如何解决
- JZOJ 5483. 【清华集训2017模拟11.26】简单路径
- (课程学习笔记)玩转Linux:常用命令实例指南
- linux软件安装非系统盘,linux操作系统可不可以像安装windows软件一样在windows系统下的硬盘上安装...
- php 变量文件间传递,同一文件的两个JS函数之间如何传变量?
- drf版本控制 和django缓存,跨域问题,
- 过滤器做权限校验以及遇到的坑
- Maven静态资源导出失败问题
- 博弈论 | 演化博弈理论(Evolutionary Game Theory)的理解
- matlab怎么导入数据格式,Matlab导入Excel文件中的数据的详细教程分享
- [BZOJ1233][Usaco2009Open]干草堆tower(单调队列优化)
- 北京图灵学院 Python(02)
- 【论文阅读笔记】BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding
- 移远BC35-G模组通过LWM2M协议接入OneNet教程
- (附源码)计算机毕业设计SSM基于web的网上订餐系统
- 易语言认识易语言数据类型
热门文章
- 使用Embedded VC++开发通讯终端及代码分析
- 重拾-Spring Transaction
- (原創) 如何解決移除DSP Builder後,在Matlab殘留錯誤訊息的問題? (SOC) (DSP Builder) (Matlab)...
- ECMAScript 6 Features 中文版
- 体验是情感的(译稿)
- 【译】Asp.net MVC 利用自定义RouteHandler来防止图片盗链 (转)
- 特斯拉致命车祸最新调查结果发布
- Zabbix监控MongoDB
- jsp常见获取地址函数之间的不同
- Selenium Web 自动化 - Selenium常用API