Sping-Spring表达式语言SpEL

概述

Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。


语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL


SpEL 为 bean 的属性进行动态赋值提供了便利.

通过 SpEL 可以实现:

  • 通过 bean 的 id 对 bean 进行引用

  • 调用方法以及引用对象中的属性

  • 计算表达式的值

  • 正则表达式的匹配


SpEL:字面量

字面量的表示:

整数:
<property name="count" value="#{5}"/>

小数:
<property name="frequency" value="#{89.7}"/>

科学计数法:
<property name="capacity" value="#{1e4}"/>

String可以使用单引号或者双引号作为字符串的定界符号:
<property name=“name” value="#{'Chuck'}"/> 
或
<property name='name' value='#{"Chuck"}'/>


Boolean:
<property name="enabled" value="#{false}"/>

如果仅仅是表示字面量,其实是没有必要使用Spring EL表达式的,这里仅仅演示一下而已,日常的开发中很少使用。


SpEL:引用 Bean、属性和方法

引用其他对象

这里写图片描述

但是我们更常用ref 来实现其他对象的引用


引用其他对象的属性

这里写图片描述


调用其他方法,还可以链式操作

这里写图片描述

这里写图片描述


调用静态方法或静态属性

通过 T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性:

这里写图片描述


SpEL支持的运算符号

算数运算符:+, -, *, /, %, ^

这里写图片描述


加号还可以用作字符串连接

这里写图片描述


比较运算符: <, >, ==, <=, >=, lt, gt, eq, le, ge

这里写图片描述

这里写图片描述


逻辑运算符号: and, or, not, |

这里写图片描述


if-else 运算符:?: (ternary), ?: (Elvis)

这里写图片描述


if-else 的变体

这里写图片描述


正则表达式:matches

这里写图片描述


示例-基于xml的方式

这里写图片描述

package com.xgj.spel;

/**
 * 
 * 
 * @ClassName: Address
 * 
 * @Description: 地址信息
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年4月7日 下午8:29:12
 */
public class Address {

    private String city;
    private String street;


    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    @Override
    public String toString() {
        return "Address [city=" + city + ", street=" + street + ", getClass()=" + getClass() + ", hashCode()=" + hashCode() + ", toString()=" + super.toString() + "]";
    }

}
package com.xgj.spel;

/**
 * 
 * 
 * @ClassName: Car
 * 
 * @Description: 车辆
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年4月7日 下午8:30:01
 */
public class Car {

    private String brand;
    private double price;
    // 调用静态方法或静态属性:通过 T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性
    private long weight;

    public long getWeight() {
        return weight;
    }

    public void setWeight(long weight) {
        this.weight = weight;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car [brand=" + brand + ", price=" + price + ", weight=" + weight + "]";
    }


}
package com.xgj.spel;

public class Boss {

    private String name;
    private Car car;
    // 通过 Spring El 引用 Address的city
    private String city;
    // 通过 Car的price属性,确定info ,如果car.price>=500000 ,info 为CEO,否则为 Staff
    private String info;

    public String getName() {
        return name;
    }

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

    public Car getCar() {
        return car;
    }

    public void setCar(Car car) {
        this.car = car;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getInfo() {
        return info;
    }

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

    @Override
    public String toString() {
        return "Boss [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info + "]";
    }

}

配置文件:

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="car" class="com.xgj.spel.Car" 
        p:brand="Bench" 
        p:price="700000"
        p:weight="#{T(java.lang.Math).PI * 4567}" />

    <!-- 通过Spring El表达式为属性赋值一个字面值 ,
           当然了,如果是字面值就没有必要使用Spring El表达式了,这里仅仅是演示该用法 -->
    <bean id="address" class="com.xgj.spel.Address" 
        p:city="#{'NanJing'}"
        p:street="RuanJianDaDao" />

    <bean id="boss" class="com.xgj.spel.Boss" 
        p:name="Artisan" 
        p:city="#{address.city}"
        p:car-ref="car"
        p:info="#{car.price > 500000 ? 'CEO' : 'staff'}" />


</beans>

测试类:

package com.xgj.spel;

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

public class SpelTest {

    public static void main(String[] args) {

        String configLocation = "com/xgj/spel/beans_spel.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);

        Car car = (Car) ctx.getBean("car");
        System.out.println(car);

        Boss boss = (Boss) ctx.getBean("boss");
        System.out.println(boss);

    }

}

结果:

2018-04-07 21:21:30,804  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4af6178d: startup date [Sat Apr 07 21:21:30 BOT 2018]; root of context hierarchy
2018-04-07 21:21:30,907  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/spel/beans_spel.xml]
Car [brand=Bench, price=700000.0, weight=14347]
Boss [name=Artisan, car=Car [brand=Bench, price=700000.0, weight=14347], city=NanJing, info=CEO]

示例-基于注解的方式

我们通过一个数据库的例子来演示。虽然可以通过Spring El 表达式从配置文件中加载一个参数值,比如

@Value("#{properties['jdbc.driverClassName']}")

是不是容易出错…. Spring提供了更好的方式 context:property-placeholder。

这里写图片描述

package com.xgj.spel.annotation;

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

/**
 * 
 * 
 * @ClassName: MyDataSource
 * 
 * @Description: 数据源 @Component标注
 * 
 * @author: Mr.Yang
 * 
 * @date: 2018年4月7日 下午9:26:32
 */

@Component
public class MyDataSource {

    private String driverClass;
    private String url;
    private String username;
    private String password;

    public String getDriverClass() {
        return driverClass;
    }

    /**
     * 
     * 
     * @Title: setDriverClass
     * 
     * @Description: @Value注解自动注入属性配置文件中对应属性的值
     * 
     * @param driverClass
     * 
     * @return: void
     */
    @Value("${jdbc.driverClassName}")
    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    public String getUrl() {
        return url;
    }

    @Value("${jdbc.url}")
    public void setUrl(String url) {
        this.url = url;
    }

    public String getUsername() {
        return username;
    }

    // @Value("$(jdbc.username)")
    @Value("${jdbc.username}")
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    @Value("${jdbc.password}")
    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "MyDataSource [driverClass=" + driverClass + ", url=" + url + ", username=" + username + ", password=" + password + "]";
    }

}
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    <!-- 扫描的基包 -->
    <context:component-scan base-package="com.xgj.spel.annotation"/>

    <!-- 加载外部properties文件 -->
    <context:property-placeholder location="classpath:mysql/db_mysql.properties"/>  

</beans>

db_mysql.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/artisan
jdbc.username=artisan
jdbc.password=artisan
package com.xgj.spel.annotation;

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

public class TestCase {

    @Test
    public void test() {

        String configurationLocation = "com/xgj/spel/annotation/beans_anno.xml";
        ApplicationContext ctx = new ClassPathXmlApplicationContext(configurationLocation);

        MyDataSource myDataSource = (MyDataSource) ctx.getBean("myDataSource");
        System.out.println(myDataSource);

        System.out.println("driverClassName:" + myDataSource.getDriverClass());
        System.out.println("url:" + myDataSource.getUrl());
        System.out.println("username:" + myDataSource.getUsername());
        System.out.println("password:" + myDataSource.getPassword());

    }

}

运行结果

2018-04-07 23:37:11,409  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@761df304: startup date [Sat Apr 07 23:37:11 BOT 2018]; root of context hierarchy
2018-04-07 23:37:11,552  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/spel/annotation/beans_anno.xml]
MyDataSource [driverClass=com.mysql.jdbc.Driver, url=jdbc:mysql://localhost:3306/artisan, username=artisan, password=artisan]
driverClassName:com.mysql.jdbc.Driver
url:jdbc:mysql://localhost:3306/artisan
username:artisan
password:artisan
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小工匠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值