您的位置:首页 > 编程语言 > Java开发

spring-SpEL的使用

2018-01-26 23:29 162 查看
SPEL(Spring Expression Language)即Spring3中功能丰富强大的表达式语言,简称SpEL。SpEL是类似于OGNL和JSF EL的表达式语言,能够在运行时构建复杂表达式,存取对象属性、对象方法调用等。所有的SpEL都支持XML和Annotation两种方式,格式:#{ SpEL expression }。

通过SpEL可以实现:

1.通过bean的id对bean进行引用

2.调用方法以及引用对象的属性

3.计算表达式得值

4.正则表达式的匹配

下面我就分别对上面四种作用进行代码展示:

Address

package com.yuehailin.spring.beans.spel;

/**
* Created by yuehailin on 2018/1/26.
*/
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 + '\'' +
'}';
}
}

Car

package com.yuehailin.spring.beans.spel;

/**
* Created by yuehailin on 2018/1/26.
*/
public class Car {
private  String brand;
private  double price;
//    轮胎的周长
private  double tyrePerimeter;

public double getTyrePerimeter() {
return tyrePerimeter;
}

public void setTyrePerimeter(double tyrePerimeter) {
this.tyrePerimeter = tyrePerimeter;
}

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 +
", tyrePerimeter=" + tyrePerimeter +
'}';
}

public Car() {
System.out.println("car");
}
}

Person

package com.yuehailin.spring.beans.spel;

/**
* Created by yuehailin on 2018/1/26.
*/
public class Person {
private  String name;
private  Car car;
//    引用address的city属性
private  String city;
//    根据car的price确定info:car的price>=300000为金领,否则为白领。
private  String info;

public String getInfo() {
return info;
}

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

public String getCity() {
return city;
}

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

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;
}

@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", car=" + car +
", city='" + city + '\'' +
", info='" + info + '\'' +
'}';
}
}


beans-spel.xml

<?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.yuehailin.spring.beans.spel.Address">
<property name="city" value="#{'BeiJing'}"></property>
<property name="street" value="daxiangzi"></property>
</bean>
<bean id="car" class="com.yuehailin.spring.beans.spel.Car">
<property name="brand" value="baoma"></property>
<property name="price" value="280000"></property>
<!--使用spel引用类的静态属性-->
<property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property>
</bean>
<bean id="person" class="com.yuehailin.spring.beans.spel.Person">
<!--使用spel来引用其他的bean-->
<property name="car" value="#{car}"></property>
<property name="city" value="#{address.city}"></property>
<property name="info" value="#{car.price>300000 ? '金领':'白领'}"></property>
<property name="name" value="岳海林"></property>
</bean>
</beans>

本人小白,如有错误,请指正。谢谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: