Spring—SpEL表达式总结

SpEL(Spring Expression Language),即Spring表达式语言。它是一种类似JSP的EL表达式、但又比后者更为强大有用的表达式语言。

为什么要用SpEL:因为它可以在spring容器内实时查询和操作数据,尤其是操作List列表型、Array数组型数据。所以使用SpEL可以有效缩减代码量,优化代码结构,笔者认为很有用。

1.  "#{…} 用于执行SpEl表达式,并将内容赋值"
2.  " ${…} 主要用于加载外部属性文件中的值"
3. "#{…} 和${…} 可以混合使用,但是必须#{}外面,${}在里面,#{ ‘${}’ } ,注意单引号,注意不能反过来"

一. 用法
常规SpEL有三种用法:

  • 在注解@Value中使用
  • 在XML配置中使用
  • 在代码中创建Expression对象,利用Expression对象来执行SpEL
  1. @Value注解
    @Value可以加在class的成员变量和形参上。用法如下
    //@Value能修饰成员变量和方法形参
    //#{}内就是SpEL表达式的语法
    //Spring会根据SpEL表达式语法,为变量arg赋值
    @Value("#{表达式}")
    public String arg;

如果修饰成员变量,Spring容器会根据SpEL表达式语法筛选修改数据,然后赋值给所@Value修饰的变量;

如果修饰方法形参,则是过滤传进来的参数值。

  1. XML配置
    XML配置用在Spring的applicationContext.xml配置文件内的<bean>元素上,用法如下:
<bean id="xxx" class="com.java.XXXXX.xx">
    <!-- 同@Value,#{}内是表达式的值,可放在property或constructor-arg内 -->
    <property name="arg" value="#{表达式}">
</bean>

用法跟注解@Value修饰形参类似

  1. Expression​​​​​​
    在代码中创建Expression对象,利用Expression对象来执行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 SpELTest {
 
    public static void main(String[] args) {
 
        //创建ExpressionParser解析表达式
        ExpressionParser parser = new SpelExpressionParser();
        //SpEL表达式语法设置在parseExpression()入参内
        Expression exp = parser.parseExpression("表达式");
        //执行SpEL表达式,执行的默认Spring容器是Spring本身的容器:ApplicationContext
        Object value = exp.getValue();
        
 
        /**也可以使用非Spring的ApplicationContext容器,则用下面的方法*/
        //创建一个虚拟的容器EvaluationContext
        StandardEvaluationContext ctx = new StandardEvaluationContext();
        //向容器内添加bean
        BeanA beanA = new BeanA();
        ctx.setVariable("bean_id", beanA);
        //setRootObject并非必须;一个EvaluationContext只能有一个RootObject,引用它的属性时,可以不加前缀
        ctx.setRootObject(XXX);        
        //getValue有参数ctx,从新的容器中根据SpEL表达式获取所需的值
        Object value = exp.getValue(ctx);
    }
}

Expression用法可以在代码中使用SpEL进行数据的读取过滤和修改,十分方便。

由上面可以看出,SpEL与Spring容器本身紧密相关,且用法特别灵活,可以直接操作Spring管理的各种bean、变量、properties配置文件等数据。

二. SpEL表达式语法
知道SpEL的使用场景范围和用法后,我们来看下SpEL表达式的具体语法。

SpEL语法决定了程序员能取到通过编写SpEL,从容器内取到什么样的数据,以及可以把数据处理成什么结果。有时用java代码写很多行的逻辑,用SpEL表达式一句话就可以搞定。

具体如下:

  1. 直接量表达式
    @Value("#{'Hello World'}")        
    String word;        //变量word赋值直接量:字符串"Hello World"
  1. 直接使用java代码
    如在SpEL中直接试用new/instance of,像写Java代码一样。注意:在SpEL中直接使用某个类名时,此类必须是java.lang 包中的类,才可以在SpEL中省略包名;否则需要写全名
Expression exp = parser.parseExpression("new Spring('Hello World')"); 
  1. 使用T(Type)
    使用“T(Type)”来表示java.lang.Class类的实例,即如同java代码中直接写类名。同样,只有java.lang 下的类才可以省略包名。此方法一般用来引用常量或静态方法
parser.parseExpression("T(Integer).MAX_VALUE");    //等同于java代码中的:Integer.MAX_VALUE
  1. 变量
    获取容器内的变量,可以使用“#bean_id”来获取。有两个特殊的变量,可以直接使用。

    "#this 使用当前正在计算的上下文"
    " #root 引用容器的root对象,即applicationContext本身"

//从ctx容器内,获取rootObject,并转换为String类型
       String result1 = parser.parseExpression("#root").getValue(ctx, String.class);  
       
       //在ctx容器内,设置abc的值为"abcdef"
       String s = new String("abcdef");
       ctx.setVariable("abc",s);
       //取id为abc的bean,然后调用其中的substring方法,得到结果赋值给result2
       String result2 = parser.parseExpression("#abc.substring(0,1)").getValue(ctx, String.class);
  1. 方法调用
    SpEL的方法调用与直接编写Java代码没有什么区别。具体可见上例abc.substring(0,1)与java代码”abcdef”.substring(0,1)效果一样

SpEL也可以自定义方法,如下:

    //创建ctx容器
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    //获取java自带的Integer类的parseInt(String)方法
    Method parseInt = Integer.class.getDeclaredMethod("parseInt", String.class);
    //将parseInt方法注册在ctx容器内
    ctx.registerFunction("parseInt", parseInt);
    //再将parseInt方法设为parseInt2
    ctx.setVariable("parseInt2", parseInt);

    //创建ExpressionParser解析表达式
    ExpressionParser parser = new SpelExpressionParser();
    //SpEL语法,比对两个方法执行完成后,结果是否相同
    String expreString = "#parseInt('2') == #parseInt2('3')";
    Expression expression = parser.parseExpression(expreString);
    return expression.getValue(ctx, Boolean.class);    //执行结果为false

    /** 如果String expreString = "#parseInt('2') == #parseInt2('3')",执行结果为true */
    /** 可见SpEL正常执行*/

“registerFunction”和“setVariable”都可以注册自定义函数,但是两个方法的含义不一样,推荐使用“registerFunction”方法注册自定义函数。

  1. 运算符表达式
    算数表达式(“1+2-3*4/2″)
    比较表达式(“1>2”)
    逻辑表达式(“2>1 and (!true or !false)”)
    赋值表达式(“#variableName=value”)
    三目表达式(“表达式1?表达式2:表达式3”)
    正则表达式(“123′ matches ‘\d{3}”)
    等运算符,都可以直接放在SpEL中,执行结果为运算符表达式的结果

  2. Elvis运算符
    是三目运算符的特殊写法,可以避免null报错的情况

    //SpEL可简写为:
    name?:"other"

    //等同于java代码
    name != null? name : "other"

  3. 安全保证
    为了避免操作对象本身可能为null,取属性时报错,可以使用SpEL检验语法

语法: “对象?.变量|方法”

//SpEL表达式简写
list?.length

//等同于java代码
list == null? null: list.length

当对象为null时,直接返回“null”,不会抛出NullPointerException

  1. 集合定义
    使用“{表达式,……}”定义List,如“{1,2,3}”
//SpEL的@Value注解设置List
@Value("1,2,3")
private List<Integer> f1;

@RequestMapping(value = "/a", method = RequestMethod.POST)
public List<Integer> a() throws NoSuchMethodException {

    //SpEL
    List<Integer> result1 = parser.parseExpression("{1,2,3}").getValue(List.class);

    //等同于如下java代码
    Integer[] integer = new Integer[]{1,2,3};
    List<Integer> result2 = Arrays.asList(integer);

    return result1;
}

对于字面量表达式列表,SpEL会使用java.util.Collections.unmodifiableList 方法将列表设置为不可修改。

  1. 集合访问
    SpEL目前支持所有集合类型和字典类型的元素访问

语法:“集合[索引]”、“map[key]”

EvaluationContext context = new StandardEvaluationContext();

//即list.get(0)
int result1 = parser.parseExpression("{1,2,3}[0]").getValue(int.class);

//list获取某一项
Collection<Integer> collection = new HashSet<Integer>();
collection.add(1);
collection.add(2);

context.setVariable("collection", collection);
int result2 = parser.parseExpression("#collection[1]").getValue(context, int.class);

//map获取
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);

context.setVariable("map", map);
int result3 = parser.parseExpression("#map['a']").getValue(context, int.class);

  1. 集合修改
    可以使用赋值表达式或Expression接口的setValue方法修改;

//赋值语句
int result = parser.parseExpression("#array[1] = 3").getValue(context, int.class);

//serValue方法
parser.parseExpression("#array[2]").setValue(context, 4);

  1. 集合选择
    通过一定的规则对及格进行筛选,构造出另一个集合

语法:“(list|map).?[选择表达式]”

选择表达式结果必须是boolean类型,如果true则选择的元素将添加到新集合中,false将不添加到新集合中。

parser.parseExpression("#collection.?[#this>2]").getValue(context, Collection.class);
上面的例子从数字的collection集合中选出数字大于2的值,重新组装成了一个新的集合

  1. 集合投影
    根据集合中的元素中通过选择来构造另一个集合,该集合和原集合具有相同数量的元素

语法:“SpEL使用“(list|map).![投影表达式]”

public class Book {

public String name;         //书名
public String author;       //作者
public String publisher;    //出版社
public double price;        //售价
public boolean favorite;    //是否喜欢

}

public class BookList {

@Autowired
protected ArrayList<Book> list = new ArrayList<Book>() ;

protected int num = 0;

}

将BookList的实例映射为bean:readList,在另一个bean中注入时,进行投影

//从readList的list下筛选出favorite为true的子集合,再将他们的name字段投为新的list
@Value("#{list.?[favorite eq true].![name]}")
private ArrayList<String> favoriteBookName;
案例
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

*/
@Slf4j
@RestController
@RequestMapping("/SpEL")
public class SpELController {

@Value("#{'Hello World1'}")
String helloWorld1;        //变量word赋值直接量:字符串"Hello World"

@Value("Hello World2")
String helloWorld2;        //变量word赋值直接量:字符串"Hello World"

//注入list
@Value("7,2,3,5,1")
private List<Integer> fList;


/**
 * {@code @Value} 注入String
 *
 * @return
 */
@RequestMapping(value = "/valueAnnoString", method = RequestMethod.POST)
public String valueAnnoString() {
    return helloWorld1 + " & " + helloWorld2;
}


/**
 * {@code @Value} 注入List
 *
 * @return
 * @throws NoSuchMethodException
 */
@RequestMapping(value = "/valueAnnoList", method = RequestMethod.POST)
public List<Integer> valueAnnoList() {
    return fList;
}


/**
 * 测试通过ExpressionParser调用SpEL表达式
 * @return
 */
@RequestMapping(value = "/expressionParse", method = RequestMethod.POST)
public List<Integer> expressionParse() {

    //创建ExpressionParser解析表达式
    ExpressionParser parser = new SpelExpressionParser();
    List<Integer> result1 = parser.parseExpression("{4,5,5,6}").getValue(List.class);
    return result1;
}



/**
 * 使用java代码
 * @return
 */
@RequestMapping(value = "/javaCode", method = RequestMethod.POST)
public Integer javaCode() {

    //创建ExpressionParser解析表达式
    ExpressionParser parser = new SpelExpressionParser();

    //等同于直接用java代码,还有方法调用
    String str = parser.parseExpression("new String('Hello World').substring(3)").getValue(String.class);
    log.info("str=={}", str);

    //TType 等同于java的Integer.MAX_VALUE
    Integer integer = parser.parseExpression("T(Integer).MAX_VALUE").getValue(Integer.class);
    log.info("integer=={}", integer);
    return integer;
}




/**
 * 注入并调用method方法
 * @return
 * @throws NoSuchMethodException
 */
@RequestMapping("methodInvoke")
private boolean methodInvoke() throws NoSuchMethodException {
    //创建ctx容器
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    //获取java自带的Integer类的parseInt(String)方法
    Method parseInt = Integer.class.getDeclaredMethod("parseInt", String.class);
    //将parseInt方法注册在ctx容器内, 推荐这样使用
    ctx.registerFunction("parseInt", parseInt);
    //再将parseInt方法设为parseInt2
    ctx.setVariable("parseInt2", parseInt);

    //创建ExpressionParser解析表达式
    ExpressionParser parser = new SpelExpressionParser();
    //SpEL语法,比对两个方法执行完成后,结果是否相同
    String expreString = "#parseInt('2') == #parseInt2('3')";
    //执行SpEL
    Expression expression = parser.parseExpression(expreString);
    Boolean value = expression.getValue(ctx, Boolean.class);
    return value;
}



/**
 * 运算符
 * @return
 */
@RequestMapping(value = "/operator", method = RequestMethod.POST)
public boolean operator() {

    //创建ctx容器
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    //将字符串defg放在 ctx容器内
    ctx.setVariable("abc", new String("defg"));
    //创建ExpressionParser解析表达式
    ExpressionParser parser = new SpelExpressionParser();
    String abc = parser.parseExpression("#abc").getValue(ctx, String.class);
    log.info("abc=={}", abc);

    //运算符判断
    Boolean result = parser.parseExpression("#abc.length() > 3").getValue(ctx, Boolean.class);
    log.info("result=={}", result);
    return result;
}


/**
 * Elvis等用法
 * @return
 */
@RequestMapping(value = "/elvis", method = RequestMethod.POST)
public void elvis(){
    //创建ctx容器
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    //将字符串defg放在 ctx容器内
    ctx.setVariable("name", null);
    //创建ExpressionParser解析表达式
    ExpressionParser parser = new SpelExpressionParser();
    String name = parser.parseExpression("#name?:'other'").getValue(ctx, String.class);
    log.info("name=={}",name);
    log.info("saved length() == {}", parser.parseExpression("#name?.lenth()").getValue(ctx));

    //将字符串defg放在 ctx容器内
    ctx.setVariable("name", "abc");
    name = parser.parseExpression("#name?:'other'").getValue(ctx, String.class);
    log.info("changed name=={}",name);
    log.info("changed saved length() == {}", parser.parseExpression("#name?.length()").getValue(ctx));


    //map获取
    Map<String, Integer> map = new HashMap<String, Integer>();
    map.put("a", 1);
    ctx.setVariable("map", map);
    int mapA = parser.parseExpression("#map['a']").getValue(ctx, int.class);
    log.info("map['a']=={}", mapA);
    //修改
    parser.parseExpression("#map['a']").setValue(ctx, 6);
    mapA = parser.parseExpression("#map['a']").getValue(ctx, int.class);
    log.info("changed map['a']=={}", mapA);

    return ;
}


@RequestMapping("/listFunction")
private void listFunction() {
    //创建ctx容器
    StandardEvaluationContext ctx = new StandardEvaluationContext();
    //创建ExpressionParser解析表达式
    ExpressionParser parser = new SpelExpressionParser();

    //list过滤
    ctx.setVariable("aList",fList);
    List<Integer> cList = parser.parseExpression("#aList.?[#this>3]").getValue(ctx, List.class);
    log.info("filter list=={}", cList);


    List<Book> books = new ArrayList<>();
    books.add(new Book("JAVA Program", 2000, 102.5));
    books.add(new Book("C Program", 1985, 36));
    books.add(new Book("scala", 2015, 60));

    //object过滤
    ctx.setVariable("books", books);
    List<Book> filterBooks1 = (List<Book>) parser.parseExpression("#books.?[year>2000]").getValue(ctx);
    log.info("filterBooks1=={}", filterBooks1);

    //投影
    List<String> filterBooksName = parser.parseExpression("#books.?[price<100].![name]").getValue(ctx, List.class);
    log.info("filterBooksName=={}", filterBooksName);

    return;
}



@Data
class Book{
    private String name;
    private int year;
    private double price;

    public Book(String name, int year, double price) {
        this.name = name;
        this.year = year;
        this.price = price;
    }
}

}
参考:
http://www.enmalvi.com/2020/07/23/spring-spel/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • 前言SpEL(Spring Expression Language),即Spring表达式语言,是比JSP的EL更...
    幽澜先生阅读 101,417评论 2 31
  • 预讲知识点 1.表达式语言的基本操作形式以及处理流程; 2.在Spring之中各种表达式字符串的编写。 具体内容 ...
    Scalelength阅读 1,077评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 4.1 介绍 Spring EL表达是一种强大的表达式语言, 可以支持在运行时查询和操作对象.它与EL类似, 但提...
    xzz4632阅读 4,275评论 0 0
  • 一. 用法 SpEL有三种用法,一种是在注解@Value中;一种是XML配置;最后一种是在代码块中使用Expres...
    夜阑人儿未静阅读 633评论 0 0