Spring學習總結(四)——表達式語言 Spring Expression Language

SpEL簡介與功能特性

Spring表達式語言(簡稱SpEL)是一個支持查詢並在運行時操縱一個對象圖的功能強大的表達式語言。SpEL語言的語法相似於統一EL,但提供了更多的功能,最主要的是顯式方法調用和基本字符串模板函數。java

同不少可用的Java 表達式語言相比,例如OGNL,MVEL和JBoss EL,SpEL的誕生是爲了給Spring社區提供一個能夠給Spring目錄中全部產品提供單一良好支持的表達式語言。其語言特性由Spring目錄中的項目需求驅動,包括基於eclipse的SpringSource套件中的代碼補全工具需求。也就是說,SpEL是一個基於技術中立的API容許須要時與其餘表達式語言集成。正則表達式

SpEL做爲Spring目錄中表達式求值的基礎,它並非直接依賴於Spring而是能夠被獨立使用。爲了可以自包含,本章中的許多示例把SpEL做爲一個獨立的表達式語言來使用。這就須要建立一些如解析器的引導基礎組件類。大多數Spring用戶只須要爲求值編寫表達式字符串而不須要關心這些基礎組件spring

SpEL功能特性:express

  • ž 字符表達式
  • ž 布爾和關係操做符
  • ž 正則表達式
  • ž 類表達式
  • ž 訪問properties,arrays,lists,maps
  • ž 方法調用
  • ž 關係操做符
  • ž 賦值
  • ž 調用構造器
  • ž 三元操做符
  • ž 變量
  • ž 用戶自定義函數
  • ž 集合投影
  • ž 集合選擇
  • ž 模板表達式

1、爲何須要Spring表達式語言

1.一、新建一個Maven Web項目,添加依賴,pom.xml以下所示:apache

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zhangguo</groupId>
    <artifactId>Spring053</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Spring053</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.4</version>
        </dependency>
    </dependencies>
</project>

表達式語言所依賴的包以下所示:編程

 2.二、爲了IOC,定義了用戶類User.java與Order.java,以下所示:數組

用戶類:安全

package com.zhangguo.Spring053.spel01;

/**
 * 訂單類
 *
 */
public class Order {
    /**
     * 訂單名稱
     */
    private String orderName;
    /*
     * 用戶姓名
     */
    private String userName;
    /**
     * 用戶對象
     */
    private User customer;

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public User getCustomer() {
        return customer;
    }

    public void setCustomer(User customer) {
        this.customer = customer;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    @Override
    public String toString() {
        return "訂單名:"+this.getOrderName()+",姓名:"+this.getUserName()+",編號:"+this.getCustomer().getId();
    }
}
View Code

訂單類:dom

package com.zhangguo.Spring053.spel01;

/**
 * 訂單類
 *
 */
public class Order {
    /**
     * 訂單名稱
     */
    private String orderName;
    /*
     * 用戶姓名
     */
    private String userName;
    /**
     * 用戶對象
     */
    private User customer;

    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public User getCustomer() {
        return customer;
    }

    public void setCustomer(User customer) {
        this.customer = customer;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}
View Code

2.三、編寫容器初始化的配置文件spel01.xml,內容以下:eclipse

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

    <bean id="gyl" class="com.zhangguo.Spring053.spel01.User" p:id="9527">
        <property name="name" value="郭永樂">
        </property>
    </bean>
    
    <bean id="order001" class="com.zhangguo.Spring053.spel01.Order">
        <property name="customer" ref="gyl"></property>
        <property name="name" value="#{gyl.name}"></property>
        <property name="orderName" value='#{"Apples".toUpperCase()}'></property>
    </bean>

</beans>

在配置文件中,出現了#{}形式的表達式,咱們就稱爲Spel表達式。#{gyl.name}做用是找到名稱爲gyl的bean取出中間的name值;#{"Apples".toUpperCase()}把字符串Apples轉換成大寫並輸出。

2.四、取出bean測試

package com.zhangguo.Spring053.spel01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("spel01.xml");
        Order order=ctx.getBean("order001",Order.class);
        System.out.println(order);
    }
}

2.五、運行結果

2、SpEL表達式Hello World!

Spring表達式語言(SpEL)從3.X開始支持,它是一種可以支持運行時查詢和操做對象圖的強大的表達式,其表達式語法相似於統一表達式語言。
SpEL支持以下表達式:
基本表達式:字面量表達式、關係,邏輯與算數運算表達式、字符串鏈接及截取表達式、三目運算、正則表達式、括號優先級表達式;
類相關表達式:類類型表達式、類實例化、instanceof表達式、變量定義及引用、賦值表達式、自定義函數、對象屬性存取及安全導航表達式、對象方法調用、Bean引用;
集合相關表達式:內聯List、內聯數組、集合,字典訪問、列表,字典,數組修改、集合投影、集合選擇;不支持多維內聯數組初始化;不支持內聯字典定義;
其餘表達式:模板表達式。

從一個Hello World!的示例開始:

package com.zhangguo.Spring053.spel02;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;

public class Test {

    public static void main(String[] args) {
        //建立SpEL表達式的解析器
        ExpressionParser parser=new SpelExpressionParser();
        //解析表達式'Hello '+' World!'
        Expression exp=parser.parseExpression("'Hello '+' World!'");
        //取出解析結果
        String result=exp.getValue().toString();
        //輸出結果
        System.out.println(result);
    }
}

運行結果:

Hello World!

從示例中能夠看出java成功的將一個字符解析出告終果,若是咱們把要解析的內容設置成1+1則會解析出2來,這裏的1+1就是一個SpEL表達式,該表達式在java中只是一個String,經過解析能夠獲得字符串自己的意見,有點類型編譯程序的感受。

3、SpEL表達式

3.一、文字表達式

支持的文字表達的類型是字符串,日期,數值(整型,實型,和十六進制),布爾和空。字符串是由單引號分隔。使用反斜槓字符轉移把一個單引號字符自己放在字符串中。

        ExpressionParser ep= new SpelExpressionParser();
        System.out.println(ep.parseExpression("'HelloWorld'").getValue());
        System.out.println(ep.parseExpression("0xffffff").getValue());
        System.out.println(ep.parseExpression("1.234345e+3").getValue());
        System.out.println(ep.parseExpression("new java.util.Date()").getValue());

運行結果:

HelloWorld
16777215
1234.345
Fri Jul 01 14:50:59 CST 2016

3.二、SPEL語言特性

3.2.一、屬性

        //建立SpEL表達式的解析器
        ExpressionParser parser=new SpelExpressionParser();
        User user=new User(9527,"周星馳");
        //解析表達式須要的上下文,解析時有一個默認的上下文
        EvaluationContext ctx = new StandardEvaluationContext();
        //在上下文中設置變量,變量名爲user,內容爲user對象
        ctx.setVariable("user", user);
        //從用戶對象中得到id並+1900,得到解析後的值在ctx上下文中
        int id = (Integer) parser.parseExpression("#user.getId() + 1900").getValue(ctx);
        System.out.println(id);

運行結果:

11427

User類在前面已定義,這裏增長了一個有參構造方法。上面的示例是調用方法,其實能夠這樣:引用對象屬性,只需使用一個句點來表示一個嵌套的屬性值,以下代碼所示:

int id = (Integer) parser.parseExpression("#user.id + 1900").getValue(ctx);

運行結果:

11427

要注意的是此時#user後再也不是一個方法而是.id,直接訪問屬性,在java中這樣作是不行的,便SpEL中容許

3.2.二、數組

        String[] students=new String[]{"tom","jack","rose","mark","lucy"};
        ctx.setVariable("students", students);
        String student = parser.parseExpression("#students[3]").getValue(ctx,
                String.class);
        System.out.println(student);

結果:mark

3.2.三、列表

        List numbers = (List) parser.parseExpression("{1,2,3,4,5}").getValue();
        System.out.println(numbers.get(2)+"");
        List listOfLists = (List) parser.parseExpression("{{'a','b'},{'x','y'}}").getValue();
        System.out.println(((List)listOfLists.get(1)).get(1));

結果:3 y

3.2.四、索引器、與字典

        User user1=new User(9001,"鄒任飛");
        User user2=new User(9002,"練汶峯");
        List<User> users=new ArrayList<User>();
        users.add(user1);
        users.add(user2);
        ctx.setVariable("users", users);
        String name = parser.parseExpression("#users[1].name").getValue(ctx,String.class);
        System.out.println(name);

結果:練汶峯

在java中訪問集合中的對象經過get(索引)而在SpEL中咱們能夠直接像數組那樣訪問對象。若是是一個字典能夠這樣:#users["tom"].id

3.2.五、方法

方法調用使用典型的Java編程語法。 你可能 還在文字調用方法。 也支持可變參數。

        String c = parser.parseExpression("'abcdef'.substring(2, 3)").getValue(String.class);
        System.out.println(c);

結果:c

3.2.六、操做符

關係操做符:使用標準的操做符號支持關係操做符:等於,不等於,小於,小於等於,大於,大於等於。
邏輯操做符:支持的邏輯操做符包括and,or和not(!),不支持&&和||。
算術操做符:加法運算符能夠用於數字,字符串和日期。減法可用於數字和日期。乘法和除法僅能夠用於。其餘支持的數學運算包括取模(%)和指數冪(^)。使用標準的運算符優先級。

關係運算符:

        //true
        boolean trueValue1 = parser.parseExpression("2 == 2").getValue(Boolean.class);
        //false
        boolean falseValue1 = parser.parseExpression("2 < -5.0").getValue(Boolean.class);
        //true
        boolean trueValue2 = parser.parseExpression("'black' < 'block'").getValue(Boolean.class);
        //false,字符xyz是否爲int類型
        boolean falseValue2 = parser.parseExpression("'xyz' instanceof T(int)").getValue(Boolean.class);
        //true,正則是否匹配
        boolean trueValue3 =parser.parseExpression("'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);
        //false
        boolean falseValue3=parser.parseExpression("'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

邏輯運算:

        // -- AND 與運算 --
        //false 
        boolean falseValue4 = parser.parseExpression("true and false").getValue(Boolean.class);
        //true,isMember方法用於測試是否爲某個對象的成員
        String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";
        boolean trueValue4 = parser.parseExpression(expression).getValue(Boolean.class);
        // -- OR 或運算--
        //true
        boolean trueValue5 = parser.parseExpression("true or false").getValue(Boolean.class);
        //true
        String expression1 = "isMember('Nikola Tesla') or isMember('Albert Einstein')";
        boolean trueValue6 = parser.parseExpression(expression).getValue( Boolean.class);
        //false
        boolean falseValue5 = parser.parseExpression("!true").getValue(Boolean.class);
        //false
        String expression2 = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')";
        boolean falseValue6 = parser.parseExpression(expression).getValue(Boolean.class);

算術運算:

// Addition
int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2
String testString =
parser.parseExpression("'test' + ' ' + 'string'").getValue(String.class); // 'test string'
// Subtraction
int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4
double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class); // -9000
// Multiplication
int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6
double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class); // 24.0
// Division
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class); // -2
double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class); // 1.0
// Modulus
int three = parser.parseExpression("7 % 4").getValue(Integer.class); // 3
int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class); // 1
// Operator precedence
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class); // -21

3.2.七、表達式支持定義bean、基於XML的配置

在xml配置中能夠自由的使用SpEL,以下所示:

<bean id="gyl" class="com.zhangguo.Spring053.spel01.User" p:id="9527">
        <property name="name" value="郭永樂">
        </property>
    </bean>

    <bean id="order001" class="com.zhangguo.Spring053.spel01.Order">
        <property name="customer" ref="gyl"></property>
        <property name="userName" value="#{gyl.name}"></property>
        <property name="orderName" value='#{"Apples".toUpperCase()}'></property>
    </bean>

    <bean id="numberGuess" class="org.spring.samples.NumberGuess">
        <!--調用靜態方法random() -->
        <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }" />
    </bean>

    <bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
        <property name="defaultLocale" value="#{ systemProperties['user.region'] }" />
    </bean>

    <bean id="numberGuess" class="org.spring.samples.NumberGuess">
        <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }" />
    </bean>
    <bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
        <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }" />
    </bean>

3.2.八、表達式支持定義bean、基於註解的配置

用戶類User.java:

package com.zhangguo.Spring053.spel03;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 用戶類
 */
@Component("user1")
public class User {
    /**
     * 編號
     */
    @Value("#{9527+100}")
    private int id;
    /**
     * 姓名
     */
    @Value("#{'Hello'.toUpperCase()}")
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

訂單類:Order.java

package com.zhangguo.Spring053.spel03;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

/**
 * 訂單類
 *
 */
@Repository("Order03")
public class Order {
    /**
     * 訂單名稱
     */
    @Value("#{'Apple訂單'}")
    private String orderName;
    /*
     * 用戶姓名
     */
    @Value("#{user1.name}")
    private String userName;
    /**
     * 用戶對象
     */
    @Value("#{user1}")
    private User customer;

    
    public String getOrderName() {
        return orderName;
    }

    public void setOrderName(String orderName) {
        this.orderName = orderName;
    }

    public User getCustomer() {
        return customer;
    }

    public void setCustomer(User customer) {
        this.customer = customer;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    
    @Override
    public String toString() {
        return "訂單名:"+this.getOrderName()+",姓名:"+this.getUserName()+",編號:"+this.getCustomer().getId();
    }
}

測試代碼Test.java

package com.zhangguo.Spring053.spel03;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhangguo.Spring053.spel03.User;

public class Test {

    public static void main(String[] args) {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("spel03.xml");
        User user1=ctx.getBean("user1",User.class);
        System.out.println(user1.getId()+","+user1.getName());
        
        Order Order03=ctx.getBean("Order03",Order.class);
        System.out.println(Order03);
    }
}

運行結果:

9627,HELLO
訂單名:Apple訂單,姓名:HELLO,編號:9627

3.2.九、操做符

        ExpressionParser ep = new SpelExpressionParser();
        // 關係操做符
        System.out.println(ep.parseExpression("5>2").getValue());
        System.out.println(ep.parseExpression("2 between {1,9}").getValue());
        // 邏輯運算符
        System.out.println(ep.parseExpression("(5>2) and (2==1)").getValue());
        // 算術操做符
        System.out.println(ep.parseExpression("100-2^2").getValue());

結果:

true
true
false
96

3.2.十、變量與賦值

變量:變量能夠在表達式中使用語法#’變量名’引用
ExpressionParser ep= new SpelExpressionParser();
//建立上下文變量
EvaluationContext ctx = new StandardEvaluationContext();
ctx.setVariable(「name」, 「Hello」);
System.out.println(ep.parseExpression("#name").getValue(ctx));

輸出:Hello
賦值:屬性設置是經過使用賦值運算符。這一般是在調用setValue中執行但也能夠在調用getValue內,也可經過」#varName=value」的形式給變量賦值。
System.out.println(ep.parseExpression("#name='Ryo'").getValue(ctx));

輸出:Ryo

4、示例下載

點擊下載

相關文章
相關標籤/搜索