Spring-10 , Spring表达式语言(SpEL)

来源:互联网 发布:php读取json内容 编辑:程序博客网 时间:2024/05/29 11:43

介绍

  1. Spring表达式语言(简称SpEl)是一个支持查询和操作运行时对象导航图功能的强大的表达式语言. 它的语法类似于传统EL,但提供额外的功能,最出色的就是函数调用和简单字符串的模板函数。

  2. 语法:
    类似EL:使用#{}作为边界符号,在#{}内的字符都被认为是spEL表达式

  3. 通过springEL可以实现;
    通过bean的id引用bean
    调用对象的方法或者引用对象的属性
    计算表达式的值
    正则表达式的匹配

  4. SpEL的主要是作用:便捷的为bean的属性动态赋值。

使用SpEL

SpEL引用bean,bean的属性,bean的方法

1.sptingEL表达式引用其他对象
<property name="car" value="#{car}"></property>

2.springEL表达式引用对象的属性值
<property name="city" value="#{address.city}"></property>

3.springEL表达式调用方法
<property name="city" value="#{address.toString()}"></property>
还可以链式操作(address.getStreet().toString())。

4.springEL表达式调用静态属性或者静态方法
通过T(),返回一个class object,通过T().XXX调用对象的静态属性或者静态方法
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80 * 2}"></property>

SpEL支持的运算符

1.算数运算
+,-,*,/,%,^

`<property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80 * 2}"></property>`

2.字符串拼接
+ 运算符可以用来拼接字符串
<property name="fullName" value="#{user.firstName+user.lastName}"></property>
3.比较运算
>,<,==,<=,>=,lt,gt,eg,le,ge
4.逻辑运算
and,or,not(!)
5.分支结构
if-else
6.正则表达式匹配
matches

<property name="email" value="#{admin.email maches '/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$' }"></property>

例如

三个bean

address.java

public class Address {    private String city;    private String street;    private String number;    //构造函数,setter,getter,toString    //....}

car.java

public class Car {    private String brand;    private Double price;    private Double tyrePerimeter;// 轮胎周长    //构造函数,setter,getter,toString    //....}

person.java

public class Person {    private String name;    private Car car;    private int age;    // 引用address的city属性    private String city;    // 根据car的price决定levle    // price 10w-* :A    // price 0-10w :B    private String levle;    public static void main(String[] args) {        ApplicationContext ctx = new ClassPathXmlApplicationContext("beanSpringEL.xml");        Person person = (Person) ctx.getBean("person");        System.out.println(person);    }    //构造函数,setter,getter,toString    //....}

bean的配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="address" class="com.tuxianchao.spring.springEL.Address">        <!--使用spEL为属性赋一个字面值 -->        <property name="city" value="#{'chengdu'}"></property>        <property name="street" value="steet1"></property>        <property name="number" value="001"></property>    </bean>    <bean id="car" class="com.tuxianchao.spring.springEL.Car">        <property name="brand" value="audi"></property>        <property name="price" value="600000"></property>        <!--调用静态属性以及只用算数运算 -->        <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80 * 2}"></property>    </bean>    <bean id="person" class="com.tuxianchao.spring.springEL.Person">        <property name="name" value="#{'zahnsgan'}"></property>        <property name="age" value="22"></property>        <!-- sptingEL表达式引用其他对象 -->        <property name="car" value="#{car}"></property>        <!-- springEL表达式引用对象的属性值 -->        <property name="city" value="#{address.city}"></property>        <!-- springEL表达式if-else运算 -->        <property name="levle" value="#{car.price>100000?'A':'B'}"></property>    </bean></beans>

结果:

这里写图片描述

0 0