Spring实战之SpEl语法实例详解

本文实例讲述了Spring实战之SpEl语法。分享给大家供大家参考,具体如下:

一 Bean

package org.crazyit.app.domain;
import java.util.Date;
public class Person
{
   private Integer id;
   private String name;
   private int height;
   public Person()
   {
   }
   // 初始化全部成员变量的构造器
   public Person(Integer id , String name , int height)
   {
      this.id = id;
      this.name = name;
      this.height = height;
   }
   // id的setter和getter方法
   public void setId(Integer id)
   {
      this.id = id;
   }
   public Integer getId()
   {
      return this.id;
   }
   // name的setter和getter方法
   public void setName(String name)
   {
      this.name = name;
   }
   public String getName()
   {
      return this.name;
   }
   // height的setter和getter方法
   public void setHeight(int height)
   {
      this.height = height;
   }
   public int getHeight()
   {
      return this.height;
   }
}

二 测试类

package lee;
import org.springframework.expression.*;
import org.springframework.expression.spel.standard.*;
import org.springframework.expression.spel.support.*;
import org.springframework.expression.common.TemplateParserContext;
import java.util.*;
import org.crazyit.app.domain.*;
public class SpELTest
{
  public static void main(String[] args)
  {
    // 创建一个ExpressionParser对象,用于解析表达式
    ExpressionParser parser = new SpelExpressionParser();
    // 使用直接量表达式
    Expression exp = parser.parseExpression("'Hello World'");
    System.out.println(exp.getValue(String.class));
    exp = parser.parseExpression("0.23");
    System.out.println(exp.getValue(Double.class));
    System.out.println("---------------------------------------------");
    //------------使用SpEL创建数组-----------
    // 创建一个数组
    exp = parser.parseExpression(
      "new String[]{'java' , 'Struts' , 'Spring'}");
    System.out.println(exp.getValue());
    // 创建二维数组
    exp = parser.parseExpression(
      "new int[2][4]");
    System.out.println(exp.getValue());
    System.out.println("---------------------------------------------");
    //------------使用SpEL创建List集合-----------
    exp = parser.parseExpression(
      "{'java' , 'Struts' , 'Spring'}");
    System.out.println(exp.getValue());
    // 创建“二维”List集合
    exp = parser.parseExpression(
      "{{'疯狂Java讲义' , '疯狂Android讲义'}, {'左传' , '战国策'}}");
    System.out.println(exp.getValue());
    System.out.println("---------------------------------------------");
    //------------使用SpEL访问List集合、Map集合的元素-----------
    List<String> list = new ArrayList<String>();
    list.add("Java");
    list.add("Spring");
    Map<String, Double> map =
      new HashMap<String, Double>();
    map.put("Java" , 80.0);
    map.put("Spring" , 89.0);
    // 创建一个EvaluationContext对象,作为SpEL解析变量的上下文
    EvaluationContext ctx = new StandardEvaluationContext();
    // 设置两个变量
    ctx.setVariable("mylist" , list);
    ctx.setVariable("mymap" , map);
    // 访问List集合的第二个元素
    System.out.println(parser
      .parseExpression("#mylist[1]").getValue(ctx));
    // 访问Map集合的指定元素
    System.out.println(parser
      .parseExpression("#mymap['Java']").getValue(ctx));
    System.out.println("---------------------------------------------");
    //------------使用SpEL调用方法-----------
    // 调用String对象的substring()方法
    System.out.println(parser
      .parseExpression("'HelloWorld'.substring(2, 5)")
      .getValue());
    list = new ArrayList<String>();
    list.add("java");
    list.add("struts");
    list.add("spring");
    list.add("hibernate");
    // 创建一个EvaluationContext对象,作为SpEL解析变量的上下文
    ctx = new StandardEvaluationContext();
    // 设置一个变量
    ctx.setVariable("mylist" , list);
    // 调用指定变量所代表的对象的subList()方法
    System.out.println(parser
      .parseExpression("#mylist.subList(1, 3)").getValue(ctx));
    System.out.println("---------------------------------------------");
    //------------在SpEL中使用运算符-----------
    list = new ArrayList<String>();
    list.add("java");
    list.add("struts");
    list.add("spring");
    list.add("hibernate");
    // 创建一个EvaluationContext对象,作为SpEL解析变量的上下文
    ctx = new StandardEvaluationContext();
    // 设置一个变量
    ctx.setVariable("mylist" , list);
    // 对集合的第一个元素进行赋值
    parser.parseExpression("#mylist[0]='疯狂Java讲义'")
      .getValue(ctx);
    // 下面将输出 疯狂Java讲义
    System.out.println(list.get(0));
    // 使用三目运算符
    System.out.println(parser.parseExpression("#mylist.size()>3?"
      + "'myList长度大于3':'myList长度不大于3'")
      .getValue(ctx));
    System.out.println("---------------------------------------------");
    //------------在SpEL中使用类型运算符-----------
    //调用Math的静态方法
    System.out.println(parser.parseExpression(
      "T(java.lang.Math).random()").getValue());
    //调用Math的静态方法
    System.out.println(parser.parseExpression(
      "T(System).getProperty('os.name')").getValue());
    System.out.println("---------------------------------------------");
    //------------在SpEL中调用构造器-----------
    // 创建对象
    System.out.println(parser.parseExpression(
      "new String('HelloWorld').substring(2, 4)")
      .getValue());
    // 创建对象
    System.out.println(parser.parseExpression(
      "new javax.swing.JFrame('测试')"
      + ".setVisible('true')").getValue());
    System.out.println("---------------------------------------------");
    //------------在SpEL中使用安全导航操作-----------
    // 使用安全操作,将输出null
    System.out.println("----" + parser.parseExpression(
      "#foo?.bar").getValue());
    // 不使用安全操作,将引发NullPointerException
//    System.out.println(parser.parseExpression(
//      "#foo.bar").getValue());
    System.out.println("---------------------------------------------");
    //------------在SpEL中对集合进行选择-----------
    list = new ArrayList<String>();
    list.add("疯狂Java讲义");
    list.add("疯狂Ajax讲义");
    list.add("疯狂iOS讲义");
    list.add("经典Java EE企业应用实战");
    // 创建一个EvaluationContext对象,作为SpEL解析变量的上下文
    ctx = new StandardEvaluationContext();
    ctx.setVariable("mylist" , list);
    // 判断集合元素length()方法的长度大于7,“疯狂iOS讲义”被剔除
    Expression expr = parser.parseExpression
      ("#mylist.?[length()>7]");
    System.out.println(expr.getValue(ctx));
    map = new HashMap<String ,Double>();
    map.put("Java" , 89.0);
    map.put("Spring" , 82.0);
    map.put("英语" , 75.0);
    ctx.setVariable("mymap" , map);
    // 判断Map集合的value值大于80,只保留前面2个Entry
    expr = parser.parseExpression
      ("#mymap.?[value>80]");
    System.out.println(expr.getValue(ctx));
    System.out.println("---------------------------------------------");
    //------------在SpEL中对集合进行投影-----------
    list = new ArrayList<String>();
    list.add("疯狂Java讲义");
    list.add("疯狂Ajax讲义");
    list.add("疯狂iOS讲义");
    list.add("经典Java EE企业应用实战");
    // 创建一个EvaluationContext对象,作为SpEL解析变量的上下文
    ctx = new StandardEvaluationContext();
    ctx.setVariable("mylist" , list);
    // 得到的新集合的元素是原集合的每个元素length()方法返回值
    expr = parser.parseExpression
      ("#mylist.![length()]");
    System.out.println(expr.getValue(ctx));
    List<Person> list2 = new ArrayList<Person>();
    list2.add(new Person(1, "孙悟空" , 162));
    list2.add(new Person(2, "猪八戒" , 182));
    list2.add(new Person(3, "牛魔王" , 195));
    ctx.setVariable("mylist2" , list2);
    // 得到的新集合的元素是原集合的每个元素name属性值
    expr = parser.parseExpression
      ("#mylist2.![name]");
    System.out.println(expr.getValue(ctx));
    System.out.println("---------------------------------------------");
    //------------在SpEL中使用表达式模板-----------
    Person p1 = new Person(1, "孙悟空" , 162);
    Person p2 = new Person(2, "猪八戒" , 182);
    expr = parser.parseExpression(
      "我的名字是#{name},身高是#{height}"
      , new TemplateParserContext());
    // 将使用p1对象name、height填充上面表达式模板中的#{}
    System.out.println(expr.getValue(p1));
    // 将使用p2对象name、height填充上面表达式模板中的#{}
    System.out.println(expr.getValue(p2));
  }
}

三 测试结果

Hello World
0.23
---------------------------------------------
[Ljava.lang.String;@2c13da15
[[I@77556fd
---------------------------------------------
[java, Struts, Spring]
[[疯狂Java讲义, 疯狂Android讲义], [左传, 战国策]]
---------------------------------------------
Spring
80.0
---------------------------------------------
llo
[struts, spring]
---------------------------------------------
疯狂Java讲义
myList长度大于3
---------------------------------------------
0.5577767377702313
Windows 10
---------------------------------------------
ll
null
---------------------------------------------
----null
---------------------------------------------
[疯狂Java讲义, 疯狂Ajax讲义, 经典Java EE企业应用实战]
{Java=89.0, Spring=82.0}
---------------------------------------------
[8, 8, 7, 15]
[孙悟空, 猪八戒, 牛魔王]
---------------------------------------------
我的名字是孙悟空,身高是162
我的名字是猪八戒,身高是182

更多关于java相关内容感兴趣的读者可查看本站专题:《Spring框架入门与进阶教程》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。