Spring学习-- SpEL表达式

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

语法类似于 EL:SpEL 使用 #{...} 作为定界符 , 所有在大括号中的字符都将被认为是 SpEL , SpEL 为 bean 的属性进行动态赋值提供了便利。

通过 SpEL 可以实现:

  • 通过 bean 的 id 对 bean 进行引用。
  • 调用方式以及引用对象中的属性。
  • 计算表达式的值
  • 正则表达式的匹配。
 1 xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5 
 6     <bean id="car" class="com.itdjx.spring.spel.Car">
 7         <property name="brand" value="#{'玛莎拉蒂'}">property>
 8         <property name="price" value="#{32000.78}">property>
 9         <property name="perimeter" value="#{T(java.lang.Math).PI * 75.8f}">property>
10     bean>
11 
12     <bean id="person" class="com.itdjx.spring.spel.Person">
13         <property name='name' value='#{"华崽儿"}'>property>
14         <property name="age" value="#{25}">property>
15         <property name="marriage" value="#{car.price > 400000 and age > 30}">property>
16         <property name="car" value="#{car}">property>
17         <property name="socialStatus" value="#{car.price > 30000 ? '金领' : '白领'}">property>
18         <property name="address" value="#{address.province + '省' + address.city + '市' + address.area + '区'}"/>
19     bean>
20 
21     <bean id="address" class="com.itdjx.spring.spel.Address">
22         <property name="province" value="#{'辽宁'}"/>
23         <property name="city" value="#{'大连'}"/>
24         <property name="area" value="#{'沙河口'}"/>
25     bean>
26 
27 beans>
 1 package com.itdjx.spring.spel;
 2 
 3 /**
 4  * @author Wáng Chéng Dá
 5  * @create 2017-03-02 13:26
 6  */
 7 public class Car {
 8 
 9     private String brand;
10 
11     private double price;
12 
13     private String perimeter;
14 
15     public String getBrand() {
16         return brand;
17     }
18 
19     public void setBrand(String brand) {
20         this.brand = brand;
21     }
22 
23     public double getPrice() {
24         return price;
25     }
26 
27     public void setPrice(double price) {
28         this.price = price;
29     }
30 
31     public String getPerimeter() {
32         return perimeter;
33     }
34 
35     public void setPerimeter(String perimeter) {
36         this.perimeter = perimeter;
37     }
38 
39     @Override
40     public String toString() {
41         return "Car{" +
42                 "brand='" + brand + '\'' +
43                 ", price=" + price +
44                 ", perimeter='" + perimeter + '\'' +
45                 '}';
46     }
47 }
 1 package com.itdjx.spring.spel;
 2 
 3 /**
 4  * @author Wáng Chéng Dá
 5  * @create 2017-03-02 13:48
 6  */
 7 public class Address {
 8 
 9     private String province;
10 
11     private String city;
12 
13     private String area;
14 
15     public String getProvince() {
16         return province;
17     }
18 
19     public void setProvince(String province) {
20         this.province = province;
21     }
22 
23     public String getCity() {
24         return city;
25     }
26 
27     public void setCity(String city) {
28         this.city = city;
29     }
30 
31     public String getArea() {
32         return area;
33     }
34 
35     public void setArea(String area) {
36         this.area = area;
37     }
38 
39     @Override
40     public String toString() {
41         return "Address{" +
42                 "province='" + province + '\'' +
43                 ", city='" + city + '\'' +
44                 ", area='" + area + '\'' +
45                 '}';
46     }
47 }
 1 package com.itdjx.spring.spel;
 2 
 3 /**
 4  * @author Wáng Chéng Dá
 5  * @create 2017-03-02 13:27
 6  */
 7 public class Person {
 8 
 9     private String name;
10 
11     private int age;
12 
13     private boolean marriage;
14 
15     private Car car;
16 
17     private String socialStatus;
18 
19     private String address;
20 
21     public String getName() {
22         return name;
23     }
24 
25     public void setName(String name) {
26         this.name = name;
27     }
28 
29     public int getAge() {
30         return age;
31     }
32 
33     public void setAge(int age) {
34         this.age = age;
35     }
36 
37     public boolean isMarriage() {
38         return marriage;
39     }
40 
41     public void setMarriage(boolean marriage) {
42         this.marriage = marriage;
43     }
44 
45     public Car getCar() {
46         return car;
47     }
48 
49     public void setCar(Car car) {
50         this.car = car;
51     }
52 
53     public String getSocialStatus() {
54         return socialStatus;
55     }
56 
57     public void setSocialStatus(String socialStatus) {
58         this.socialStatus = socialStatus;
59     }
60 
61     public String getAddress() {
62         return address;
63     }
64 
65     public void setAddress(String address) {
66         this.address = address;
67     }
68 
69     @Override
70     public String toString() {
71         return "Person{" +
72                 "name='" + name + '\'' +
73                 ", age=" + age +
74                 ", marriage=" + marriage +
75                 ", car=" + car +
76                 ", socialStatus='" + socialStatus + '\'' +
77                 ", address='" + address + '\'' +
78                 '}';
79     }
80 }
package com.itdjx.spring.spel;

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

/**
 * @author Wáng Chéng Dá
 * @create 2017-03-02 13:42
 */
public class Main {

    public static void main(String[] args) {

        ApplicationContext app = new ClassPathXmlApplicationContext("spel-beans.xml");
        Person person = (Person) app.getBean("person");
        System.out.println(person);

    }
}

 

控制台输出:

Person{name='华崽儿', age=25, marriage=false, car=Car{brand='玛莎拉蒂', price=32000.78, perimeter='238.13273272948624'}, socialStatus='金领', address='辽宁省大连市沙河口区'}

 

 SpEL 字面量:

  • 整数:#{8}
  • 小数:#{8.8}
  • 科学计数法:#{1e4}
  • String:可以使用单引号或者双引号作为字符串的定界符号。
  • Boolean:#{true}

SpEL引用bean , 属性和方法:

  • 引用其他对象:#{car}
  • 引用其他对象的属性:#{car.brand}
  • 调用其它方法 , 还可以链式操作:#{car.toString()}
  • 调用静态方法静态属性:#{T(java.lang.Math).PI}

 SpEL支持的运算符号:

  • 算术运算符:+,-,*,/,%,^(加号还可以用作字符串连接)
  • 比较运算符:< , > , == , >= , <= , lt , gt , eg , le , ge
  • 逻辑运算符:and , or , not , |
  • if-else 运算符(类似三目运算符):?:(temary), ?:(Elvis)
  • 正则表达式:#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}'}

 

转载于:https://www.cnblogs.com/goodcheap/p/6490896.html

你可能感兴趣的:(java)