spring-表达式语言-SpEL【转】
- Spring表达式语言的入门介绍
- Spring表达式语言的操作范围
- Spring表达式语言的运算符
- Spring表达式语言的集合操作
- 基本表达式
- 类相关表达式
- 集合相关表达式
- 其他表达式
- public class SpelTest {
- public static void main(String[] args){
- //创建解析器
- ExpressionParser parser=new SpelExpressionParser();
- //解析表达式
- Expression expression=
- parser.parseExpression("('Hello'+'World').concat(#end)");
- //构造上下文
- EvaluationContext context=new StandardEvaluationContext();
- //为end参数值来赋值
- context.setVariable("end","!");
- //打印expression表达式的值
- System.out.println(expression.getValue(context));
- }
- }
- 表达式:表达式语言的核心,即“干什么”
- 解析器:用于将字符串表达式解析为表达式对象,即“谁来干”
- 上下文:表达式语言执行的环境,该环境可能定义变量,可能定义自定义函数,也可以提供类型转换等等,即“在哪里干”
- 根对象即活动上下文对象:根对象是默认的活动上下文对象,活动上下文对象表示了当前操作对象。即“对谁干”
- ExpressionParser接口:表示解析器
- EvaluationContext接口:表示上下文环境
- Expression接口:表示的是表达式对象
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd
- ">
- <bean id="world" class="java.lang.String">
- <constructor-arg value="#{' World!'}"/>
- </bean>
- <!--方式一-->
- <bean id="hello1" class="java.lang.String">
- <constructor-arg value="#{'Hello '}#{world}"/>
- </bean>
- <!--方式二
- 注意:Spring语言不支持嵌套,即在一个#之内又有一个#
- <constructor-arg value="#{'Hello '#{world}}"/>-->
- <bean id="hello2" class="java.lang.String">
- <constructor-arg value="#{'Hello '+world}"/>
- </bean>
- <!--方式三-->
- <bean id="hello3" class="java.lang.String">
- <constructor-arg value="#{'Hello '+@world}"/>
- </bean>
- </beans>
- public class XmlExpression {
- public static void main(String[] args){
- ApplicationContext ctx=
- new FileSystemXmlApplicationContext("src/conf/conf-spel.xml");
- String hello1=ctx.getBean("hello1",String.class);
- String hello2=ctx.getBean("hello2",String.class);
- String hello3=ctx.getBean("hello3",String.class);
- System.out.println(hello1);
- System.out.println(hello2);
- System.out.println(hello3);
- }
- }
- <!--开启注解支持-->
- <context:annotation-config/>
- <bean id="hellobean1" class="cn.lovepi.chapter05.spel.AnnoExpression"/>
- <bean id="hellobean2" class="cn.lovepi.chapter05.spel.AnnoExpression">
- <property name="value" value="haha"/>
- </bean>
- public class AnnoExpression {
- @Value("#{'Hello '+world}")
- private String value;
- public String getValue() {
- return value;
- }
- public void setValue(String value) {
- this.value = value;
- }
- public static void main(String[] args){
- ApplicationContext ctx=
- new FileSystemXmlApplicationContext("src/conf/conf-spel.xml");
- AnnoExpression hellobean1=ctx.getBean("hellobean1",AnnoExpression.class);
- AnnoExpression hellobean2=ctx.getBean("hellobean2",AnnoExpression.class);
- System.out.println(hellobean1.getValue());
- System.out.println(hellobean2.getValue());
- }
- }
通过结果可以看出:使用参数注入方式注入的值会覆盖Spring表达式所编写的值
- 字面值:最简单的一种值,即基本类型的表达式。包含的类型是字符串、数字类型(int、lang、float、double、boolean、null)字符串使用单引号分割,使用反斜杠字符转义。
- Bean以及Bean的属性或方法:通过id来引入其他的bean或者bean的属性或方法
- 类的方法和常量:在SpEL中是由T运算符调用类的方法和常量
- public class SpelLiteral {
- private int count;
- private String message;
- private float frequency;
- private float capacity;
- private String name1;
- private String name2;
- private boolean enabled;
- public int getCount() {
- return count;
- }
- public void setCount(int count) {
- this.count = count;
- }
- public String getMessage() {
- return message;
- }
- public void setMessage(String message) {
- this.message = message;
- }
- public float getFrequency() {
- return frequency;
- }
- public void setFrequency(float frequency) {
- this.frequency = frequency;
- }
- public float getCapacity() {
- return capacity;
- }
- public void setCapacity(float capacity) {
- this.capacity = capacity;
- }
- public String getName1() {
- return name1;
- }
- public void setName1(String name1) {
- this.name1 = name1;
- }
- public String getName2() {
- return name2;
- }
- public void setName2(String name2) {
- this.name2 = name2;
- }
- public boolean isEnabled() {
- return enabled;
- }
- public void setEnabled(boolean enabled) {
- this.enabled = enabled;
- }
- }
可以看到该Bean有很多基本数据类型以及String类型的属性,接下来我们一一在配置文件中为这些属性使用Spring表达式语言赋值
- <bean id="spelliteral" class="cn.lovepi.chapter05.spel.SpelLiteral">
- <property name="count" value="#{5}"/>
- <property name="message" value="The value is #{5}"/>
- <property name="frequency" value="#{89.7}"/>
- <property name="capacity" value="#{1e4}"/>
- <property name="name1" value="#{'wang'}"/>
- <property name="name2" value='#{"wang"}'/>
- <property name="enabled" value="#{false}"/>
- </bean>
- public class SpelMain {
- public static void main(String[] args){
- testSpelLiteral();
- }
- private static void testSpelLiteral(){
- ApplicationContext ctx=
- new FileSystemXmlApplicationContext("src/conf/conf-spel.xml");
- SpelLiteral literal=ctx.getBean("spelliteral",SpelLiteral.class);
- System.out.println("count= "+literal.getCount());
- System.out.println("message= "+literal.getMessage());
- System.out.println("frequency= "+literal.getFrequency());
- System.out.println("capacity= "+literal.getCapacity());
- System.out.println("name1= "+literal.getName1());
- System.out.println("name2= "+literal.getName2());
- System.out.println("enabled= "+literal.isEnabled());
- }
- }
- <property name="bean2" value="#{bean1}"/>
这句话等价与
- <property name="bean2" ref="bean1"/>
- <bean id="bean2" class="cn.lovepi.***">
- <property name="name" value="#{bean1.name}"/>
- </bean>
以上的代码等价于
- Bean2 bean2=new Bean2();
- bean2.setName(bean1.getName());
- <property name="name" value="#{bean1.getName()}/>
还可以将获取到的name值转换为大写
- <property name="name" value="#{bean1.getName().toUpperCase()}/>
- <property name="name" value="#{bean1?.getName().toUpperCase()}/>
- public class SpelClass {
- private float pi;
- private float randomNumber;
- public float getPi() {
- return pi;
- }
- public void setPi(float pi) {
- this.pi = pi;
- }
- public float getRandomNumber() {
- return randomNumber;
- }
- public void setRandomNumber(float randomNumber) {
- this.randomNumber = randomNumber;
- }
- }
为其编写配置文件
- <bean id="spelClass" class="cn.lovepi.chapter05.spel.SpelClass">
- <property name="pi" value="#{T(java.lang.Math).PI}"/>
- <property name="randomNumber" value="#{T(java.lang.Math).random()}"/>
- </bean>
- private static void testSpelClass(){
- ApplicationContext ctx=
- new FileSystemXmlApplicationContext("src/conf/conf-spel.xml");
- SpelClass spelClass=ctx.getBean("spelClass",SpelClass.class);
- System.out.println("PI="+spelClass.getPi());
- System.out.println("randomNumber="+spelClass.getRandomNumber());
- }
| 运算符类型 |
运算符示例
|
| 数值运算 | +、-、*、/、%、^(乘方运算) |
| 比较运算 | <(lt)、>(gt)、==(eg)、<=(le)、>=(ge) |
| 逻辑运算 | and、or、not、| |
| 条件运算 | ?:(ternary)、?:(Elvis) |
| 正则表达式 | matches |
- public class SpelCounter {
- private float total;
- private float count;
- public float getTotal() {
- return total;
- }
- public void setTotal(float total) {
- this.total = total;
- }
- public float getCount() {
- return count;
- }
- public void setCount(float count) {
- this.count = count;
- }
- }
接下来创建一个运算演示Bean
- public class SpelMath {
- private float ajustedAcount;
- private float circumFference;
- private float average;
- private float remainder;
- private float area;
- private String fullName;
- public float getAjustedAcount() {
- return ajustedAcount;
- }
- public void setAjustedAcount(float ajustedAcount) {
- this.ajustedAcount = ajustedAcount;
- }
- public float getCircumFference() {
- return circumFference;
- }
- public void setCircumFference(float circumFference) {
- this.circumFference = circumFference;
- }
- public float getAverage() {
- return average;
- }
- public void setAverage(float average) {
- this.average = average;
- }
- public float getRemainder() {
- return remainder;
- }
- public void setRemainder(float remainder) {
- this.remainder = remainder;
- }
- public float getArea() {
- return area;
- }
- public void setArea(float area) {
- this.area = area;
- }
- public String getFullName() {
- return fullName;
- }
- public void setFullName(String fullName) {
- this.fullName = fullName;
- }
- }
在配置文件中使用SpEL表达式语言运算符来对相应的数据进行运算赋值操作
- <bean id="spelCounter" class="cn.lovepi.chapter05.spel.SpelCounter">
- <property name="count" value="#{10}"/>
- <property name="total" value="#{100}"/>
- </bean>
- <bean id="spelMath" class="cn.lovepi.chapter05.spel.SpelMath">
- <!--加法运算符-->
- <property name="ajustedAcount" value="#{spelCounter.total+53}"/>
- <!--乘法运算符-->
- <property name="circumFference" value="#{2*T(java.lang.Math).PI*spelCounter.total}"/>
- <!--除法运算符-->
- <property name="average" value="#{spelCounter.total/spelCounter.count}"/>
- <!--取余运算符-->
- <property name="remainder" value="#{spelCounter.total%spelCounter.count}"/>
- <!--乘方运算符-->
- <property name="area" value="#{T(java.lang.Math).PI * spelCounter.total^2}"/>
- <!--字符串拼接-->
- <property name="fullName" value="#{'icarus'+' '+'wang'}"/>
- </bean>
接下来在程序入口出测试程序运行结果
- private static void testSpelMath(){
- ApplicationContext ctx=
- new FileSystemXmlApplicationContext("src/conf/conf-spel.xml");
- SpelMath math=ctx.getBean("spelMath",SpelMath.class);
- System.out.println("AjustedAcount= "+math.getAjustedAcount());
- System.out.println("CircumFference= "+math.getCircumFference());
- System.out.println("Average= "+math.getAverage());
- System.out.println("Area= "+math.getArea());
- System.out.println("Remainder= "+math.getRemainder());
- System.out.println("FullName= "+math.getFullName());
- }
- <property name="name" value="#{person.name!=null ? person.name : 'icarus'}"/>
但上面的的语句重复使用了两次person.name属性,SpEL为我们提供了一种更简便的方式:
- <property name="name" value="#{person.name!=null ?: 'icarus'}"/>
- <property name="validEmail" value="#{admin.email matches '[0-9A-Za-z_%.*+-]+@[0-9A-Za-z.-]+\\.com'}"/>
四.Spring表达式语言的集合操作
- 访问集合成员
- 查询集合成员
- 投影集合
- public class SpelCity{
- private String name;
- private String state;
- private int population;
- }
接下来我们创建一个集合,集合中的元素是SpelCity
- <util:list id="cities">
- <bean class="cn.lovepi.***.SpelCity">
- <p:namep:name="Chicago" p:state="IL" p:population="2853114">
- <bean class="cn.lovepi.***.SpelCity">
- <p:namep:name="LasCryces" p:state="NM" p:population="91865">
- </util:list>
- <property name="bigCities" value="#{cities.?[population gt 100000]}"/>
- .^[]:查询符合条件的第一个元素
- .$[]:查询符合条件的最后一个元素
- <property name="cityName1" value="#{cities.![name]}}"/>
- <property name="cityName2" value="#{cities.![name+','+state]}}"/>
- <property name="cityName3" value="#{cities.?[population gt 100000].![name+','+state]}}"/>
spring-表达式语言-SpEL【转】的更多相关文章
- Spring表达式语言SpEL
Spring表达式语言,简称SpEL,是一个支持运行时查询和操作对象图的强大的表达式语言.语法类似于EL:SpEL使用#{…}作为定界符,所有在大括号中的字符都将被认为是SpEL SpEL为bean属 ...
- Spring学习笔记--Spring表达式语言SpEL
Spring3引入了Spring表达式语言(Spring Expression Language,SpEL).SpEL是一种强大的.简洁的装配Bean的方式,它通过运行期执行的表达式将值装配到Bean ...
- Spring表达式语言SpEL简单介绍
Spring3引入了Spring表达式语言(Spring Expression Language,SpEL). SpEL有非常多特性.比較经常使用的包含: 1.使用bean的id来引用bean, 以下 ...
- Spring表达式语言:SpEl
概念: 是一个支持运行时查询和操作的对象图的强大的表达式语言. 语法类似于EL:SpEl使用#{ ...}作为定界符,所有在大括号中的 字符都将被认为是SpEl SpEl为bean的属性进行动态赋值提 ...
- Sping表达式语言--SpEL
Spring表达式语言---SpEL 是一个支持运行时查询和操作对象的强大的表达式语言 语法类似于EL:SpEL使用#{...}作为定界符,所有在大括号中的字符都将被认为是SpEL SpEL为bean ...
- Spring ——表达式语言 Spring Expression Language (转载)
目录 SpEL简介与功能特性 一.为什么需要Spring表达式语言 二.SpEL表达式Hello World! 三.SpEL表达式 3.1.文字表达式 3.2.SPEL语言特性 3.2.1.属性 3. ...
- Spring表达式语言 之 5.1 概述 5.2 SpEL基础(拾叁)
5.1 概述 5.1.1 概述 Spring表达式语言全称为"Spring Expression Language",缩写为"SpEL",类似于Struts ...
- 开涛spring3(5.1&5.2) - Spring表达式语言 之 5.1 概述 5.2 SpEL基础
5.1 概述 5.1.1 概述 Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言,能在运行 ...
- 7 -- Spring的基本用法 -- 12... Spring 3.0 提供的表达式语言(SpEL)
7.12 Spring 3.0 提供的表达式语言(SpEL) Spring表达式语言(简称SpEL)是一种与JSP 2 的EL功能类似的表达式语言,它可以在运行时查询和操作对象图.支持方法调用和基本字 ...
- Spring表达式语言之SpEL
•Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言. •语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL ...
随机推荐
- 【原创】Weblogic 反序列化远程命令执行漏洞GetShell&Cmd Exploit
这工具写到半夜四点,做个记录. 已发布至freebuf,链接:http://www.freebuf.com/vuls/90802.html
- Google 地图 API V3 之 叠加层
Google官方教程: Google 地图 API V3 使用入门 Google 地图 API V3 针对移动设备进行开发 Google 地图 API V3 之事件 Google 地图 API V3 ...
- sobel算子的一些细节
1. 形式 Gy 上下颠倒的 (*A表示卷积图像,忽略先): 看得出来,sobel算子感觉并不统一,特别是方向,我们知道matlab的图像格式是,x轴从左到右,y轴从上到下,原点在左上角. 所以,第二 ...
- tyvj1087 sumsets
背景 广东汕头聿怀初中 Train#2 Problem1 描述 正整数N可以被表示成若干2的幂次之和.例如,N = 7时,共有下列6种不同的方案:1) 1+1+1+1+1+1+12) 1+1+ ...
- JSP动作元素之include
采用include指令导入的页面输入静态导入,采用<jsp:include-/>指令属于动态导入. 语法格式如下: <jsp:include page="{relative ...
- 大组合数:Lucas定理
最近碰到一题,问你求mod (p1*p2*p3*……*pl) ,其中n和m数据范围是1~1e18 , l ≤10 , pi ≤ 1e5为不同的质数,并保证M=p1*p2*p3*……*pl ≤ 1e18 ...
- 商品库存“存取设计”,MySQL事务、表锁、行锁
MySQL 使用 SELECT ... FOR UPDATE 做事务写入前的确认 以MySQL 的InnoDB 为例,预设的 Tansaction isolation level 为 REPEATA ...
- Python cumsums和cumprod函数
>>>a = np.array([1,2,3],[4,5,6]]) >>>a array([[1,2,3], [4,5,6]]) >>>a.cum ...
- HDU 5686 斐波那契数列、Java求大数
原题:http://acm.hdu.edu.cn/showproblem.php?pid=5686 当我们要求f[n]时,可以考虑为前n-1个1的情况有加了一个1. 此时有两种情况:当不适用第n个1进 ...
- Java类名.class和getClass()区别
区别 类名.class叫做“类字面量”,因class是关键字, 所以类名.class编译时确定. getclass()运行时根据实际实例确定,getClass()是动态而且是final的. Strin ...