SPEL 详细说明:
http://docs.spring.io/spring/docs/4.3.0.BUILD-SNAPSHOT/spring-framework-reference/htmlsingle/#expressions
例子:
Address.java
package com.spring.spel;
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.java
package com.spring.spel;
public class Car {
private String brand;
private double price;
private double 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;
}
public double getTyrePerimeter() {
return tyrePerimeter;
}
public void setTyrePerimeter(double tyrePerimeter) {
this.tyrePerimeter = tyrePerimeter;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter=" + tyrePerimeter + "]";
}
}
Person.java
package com.spring.spel;
public class Person {
private String name;
private Car car;
//引用ddress bean的city属性
private String city;
//根据car的price 确定info: car的price > 300000
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 "Person [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info + "]";
}
}
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" 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"> <!-- 使用SpEL 来为属性赋值 --> <bean id="address" class="com.spring.spel.Address" p:city="#{'SH'}" p:street="lianHangRoad" /> <!-- 使用SpEL 来引用类的静态属性 --> <bean id="car" class="com.spring.spel.Car" p:brand="AUDI" p:price="#{200000}" p:tyrePerimeter="#{T(java.lang.Math).PI*2*80}" > </bean> <!-- 1. 使用SpEL 来应用其他Bean的属性—>p:city. 2. 使用SpEL 来应用其他Bean->p:car 3. 使用SpEL 运算算术符 --> <bean id="person" class="com.spring.spel.Person" p:city="#{address.city}" p:car="#{car}" p:name="#{'Matt'}" p:info="#{car.price > 300000 ? '白领' : '蓝领'}" /> </beans>
Main.java
package com.spring.spel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ext = new ClassPathXmlApplicationContext("spEL.xml");
Address address = (Address) ext.getBean("address");
System.out.println(address);
Car car = (Car) ext.getBean("car");
System.out.println(car);
Person person = (Person) ext.getBean("person");
System.out.println(person);
}
}
运行结果:
Address [city=SH, street=lianHangRoad]
Car [brand=AUDI, price=200000.0, tyrePerimeter=502.6548245743669]
Person [name=Matt, car=Car [brand=AUDI, price=200000.0, tyrePerimeter=502.6548245743669], city=SH, info=蓝领]