表达式所有操作都是可以以变量形式出现的。

观察变量的定义:

package com.Spring.ELDemo;
import org.springframework.expression.EvaluationContext;
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 TestSimple2 { public static void main(String[] args) { ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("#myevl");
EvaluationContext context=new StandardEvaluationContext();
context.setVariable("myevl", "hello");
System.out.println(exp.getValue(context));
}
}

如果要想设置变量,必须需要依靠“StandardEvaluationContext”类完成,而这个类的子类的构造方法也可以接收一个变量:

public StandardEvaluationContext(Object   rootObject)

这个构造方法表示的是设置根变量的内容:整个变量体系之中 会自动存在一个“#root”的根变量

观察如下:

package com.Spring.ELDemo;
import org.springframework.expression.EvaluationContext;
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 TestSimple2 { public static void main(String[] args) { ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("#root");
EvaluationContext context=new StandardEvaluationContext("hello");
System.out.println(exp.getValue(context));
}
}

如上,没有调用context.setVariable("#root", "hello");,也就是说直接通过了构造方法完成了根变量#root值的传递。

范例:进行比较:

package com.Spring.ELDemo;
import org.springframework.expression.EvaluationContext;
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 TestSimple2 { public static void main(String[] args) { ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("#root=='hello' ? '你好':'大家好'");
EvaluationContext context=new StandardEvaluationContext("hello");
System.out.println(exp.getValue(context));
}
}

结果:您好。

大部分情况下,最多的时候使用一下自定义变量进行使用,而不是使用根变量。

还能对方法进行引用操作。

方法引用:

范例:将Integer.parseInt()方法设置为myInt()的引用(可以结合之前java知识复习)

package com.Spring.ELDemo;
import java.lang.reflect.Method;
import org.springframework.expression.EvaluationContext;
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 TestSimple2 { public static void main(String[] args) throws Exception { ExpressionParser parser=new SpelExpressionParser();
//通过反射找到Integer.parseInt()方法的对象
Method met=Integer.class.getMethod("parseInt", String.class);
Expression exp=parser.parseExpression("#myInt('123')");
//利用StandardEvaluationContext类进行方法的引用注册
StandardEvaluationContext context=new StandardEvaluationContext();
//方法进行引用,注册
context.registerFunction("myInt", met);
System.out.println(exp.getValue(context));
}
}

注意以上方法引用步骤。

使用myInt方法在表达式之中相当于调用了Integer.parseInt()方法实现类字符串与int数据的互相转型操作。

还可以调用类中属性,以date类为例,里面有一个getTime()方法,可以将date数据变成lang型数据。

范例:调用属性

package com.Spring.ELDemo;
import java.util.Date;
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 TestSimple2 { public static void main(String[] args) throws Exception { ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("time");
//利用StandardEvaluationContext类进行方法的引用注册
StandardEvaluationContext context=new StandardEvaluationContext(new Date());
System.out.println(exp.getValue(context));
}
}

此时将date类的对象设置到了根变量,所以一旦表达式出现了“time”单词就表示,要调用getTime方法

所以需要特别提醒的是,第一个字母不区分大小写。

这种调用本身有风险,此时调用必须有前提:根变量有内容

范例:根变量为空

public class TestSimple2 {

    public static void main(String[] args) throws Exception {

        ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("time");
//利用StandardEvaluationContext类进行方法的引用注册
StandardEvaluationContext context=new StandardEvaluationContext();
System.out.println(exp.getValue(context));
}
}

根变量为空就会出现异常:

最好解决方式不是增加什么判断,而是使用Groovy安全导航操作,利用Groovy安全运算符避免空异常。

范例:使用“?. ”范围属性

package com.Spring.ELDemo;
import java.util.Date;
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 TestSimple2 { public static void main(String[] args) throws Exception { ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("#root?.time");
//利用StandardEvaluationContext类进行方法的引用注册
StandardEvaluationContext context=new StandardEvaluationContext();
System.out.println(exp.getValue(context));
}
}

结果:

如果此时根变量内容为空,那么返回就是null。

以上所有操作变量都是程序中定义的,也可以引用在applicationContext.xml文件中内容。

范例:引用配置中的变量

定义一个Message类

package com.Spring.vo;

public class Message {

    private String info;

    public String getInfo() {
return info;
} public void setInfo(String info) {
this.info = info;
}
}

在applicationcontext.xml里面配置这个类

    <bean id="msg" class="com.Spring.vo.Message">
<property name="info" value="hello word"></property>
</bean>

现在msg对象里面的info属性里面是包含配置内容的,随后希望可以在表达式里面去引用这部分的内容。

引入配置内容,如果要进行导入外部配置,使用“@名称.方法()”

package com.Spring.ELDemo;
import java.util.Date; import org.springframework.context.ApplicationContext;
import org.springframework.context.expression.BeanFactoryResolver;
import org.springframework.context.support.ClassPathXmlApplicationContext;
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 TestApplication { public static void main(String[] args) throws Exception { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
ExpressionParser parser=new SpelExpressionParser();
//格式,@配置bean的ID.方法()
Expression exp=parser.parseExpression("@msg.getInfo()");
StandardEvaluationContext context=new StandardEvaluationContext();
//将这个配置文件的内容读取交给上下文
context.setBeanResolver(new BeanFactoryResolver(ctx));
System.out.println(exp.getValue(context));
}
}

运行结果:

相当于此时,在外部配置的对象,都可以直接在表达式中使用了。并且利用表达式的语法调用对象所提供的方法。

14-spring学习-变量操作的更多相关文章

  1. spring学习(三) ———— spring事务操作

    前面一篇博文讲解了什么是AOP.学会了写AOP的实现,但是并没有实际运用起来,这一篇博文就算是对AOP技术应用的进阶把,重点是事务的处理. --wh 一.jdbcTemplate 什么是JdbcTem ...

  2. spring 学习(四): spring 的 jdbcTemplate 操作

    spring 学习(四): spring 的 jdbcTemplate 操作 spring 针对 javaee 的每一层,都提供了相应的解决技术,jdbcTemplate 的主要操作在 dao 层. ...

  3. SAS学习笔记之《SAS编程与数据挖掘商业案例》(3)变量操作、观测值操作、SAS数据集管理

    SAS学习笔记之<SAS编程与数据挖掘商业案例>(3)变量操作.观测值操作.SAS数据集管理 1. SAS变量操作的常用语句 ASSIGNMENT 创建或修改变量 SUM 累加变量或表达式 ...

  4. spring学习12 -Spring 框架模块以及面试常见问题注解等

    以下为spring常见面试问题: 1.Spring 框架中都用到了哪些设计模式? Spring框架中使用到了大量的设计模式,下面列举了比较有代表性的: 代理模式—在AOP和remoting中被用的比较 ...

  5. Spring学习笔记(一)

    Spring学习笔记(一) 这是一个沉淀的过程,大概第一次接触Spring是在去年的这个时候,当初在实训,初次接触Java web,直接学习SSM框架(当是Servlet都没有学),于是,养成了一个很 ...

  6. Spring学习1:Spring基本特性

    http://longliqiang88.github.io/2015/08/14/Spring%E5%AD%A6%E4%B9%A01%EF%BC%9ASpring%E5%9F%BA%E6%9C%AC ...

  7. 【转】Spring学习---Bean配置的三种方式(XML、注解、Java类)介绍与对比

    [原文]https://www.toutiao.com/i6594205115605844493/ Spring学习Bean配置的三种方式(XML.注解.Java类)介绍与对比 本文将详细介绍Spri ...

  8. 不错的Spring学习笔记(转)

    Spring学习笔记(1)----简单的实例 ---------------------------------   首先需要准备Spring包,可从官方网站上下载.   下载解压后,必须的两个包是s ...

  9. Spring学习(五)——Spring注解(一)

    ---恢复内容开始--- 概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射 ...

随机推荐

  1. hdu 1565 最小割

    黑白染色,源指向白,黑指向汇,容量都是方格中数的大小,相邻的格子白指向黑,容量为oo,然后求一次最小割. 这个割是一个简单割,如果只选择不在割中的点,那么一种割就和一个选数方案一一对应,割的大小就是不 ...

  2. UVA 10531 Maze Statistics 迷宫统计 迷宫插头DP 四联通 概率

    题意: 有一个N*M的图,每个格子有独立概率p变成障碍物.你要从迷宫左上角走到迷宫右下角.求每个格子成为一个有解迷宫中的障碍物的概率.N <= 5,M <= 6 分析: 这真是一道好题,网 ...

  3. poj 3624 Charm Bracelet 背包DP

    Charm Bracelet Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=3624 Descripti ...

  4. React Native中树 TreeView 实现(2)

    接上文,剩下的展示工作是重中之重,首先确定节点的布局草稿——也就是如何render item: 在此之前还有一个重要的问题就是选择何种组件盛放展示子结点,一般有如下两种: 使用scrollview加载 ...

  5. BZOJ 2301: [HAOI2011]Problem b (莫比乌斯反演)

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 436  Solved: 187[Submit][S ...

  6. Changing the Output Voltage of a Switching Regulator on the Fly

    http://www.powerguru.org/changing-the-output-voltage-of-a-switching-regulator-on-the-fly/ There are ...

  7. 【微信小程序】view顶部固定或底部固定 + scroll-view中的元素view也可以使用position:fixed;固定选中元素位置

    1.顶端固定核心代码如下: <view class="page__hd" style="position:fixed; top:0;width: 750rpx;&q ...

  8. hadoop下c++程序-天气实例

    非常希望能在hadoop上做c++程序.自己对c++还是有点情节的,依据<hadoop权威指南中文第二版>Hadoop的Pipes进行了试验,并測试成功 #include <algo ...

  9. 重新总结flex布局(flex,flex-direction,justify-content,align-items,flex-wrap,align-self)

    1.flex,主要就是按比例分配.(例如:两个div的flex:1,就大小相等) .box1{ flex:1; background-color: red; } .box2{ flex:1; back ...

  10. Flink 靠什么征服饿了么工程师?

    Flink 靠什么征服饿了么工程师? 2018-08-13    易伟平 阿里妹导读:本文将为大家展示饿了么大数据平台在实时计算方面所做的工作,以及计算引擎的演变之路,你可以借此了解Storm.Spa ...