Spring表达式语言
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。
语法类似于 EL:SpEL 使用 #{...} 作为定界符 , 所有在大括号中的字符都将被认为是 SpEL,SpEL 为 bean 的属性进行
动态赋值提供了便利
创建要使用的三个类:package com. fafa. spring. beans. 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 + "]" ;
}
}
package com. fafa. spring. beans. 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 + "]" ;
}
public Car ( ) {
System. out. println ( "Car's constructor...." ) ;
}
}
package com. fafa. spring. beans. spel;
public class Person {
private String name;
private String city;
private String info;
private Car car;
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 + ", city="
+ city + ", info=" + info + ", car=" + car + "]" ;
}
}
创建beans-spel.xml
配置文件 常用的字面量:整数,小数,科学计数法,String,Boolean
< bean id = " address" class = " com.fafa.spring.beans.spel.Address" >
< property name = " city" value = " #{' BeiJing' }" > </ property>
< property name = " street" value = " WuDaoKou" > </ property>
</ bean>
< bean id = " car" class = " com.fafa.spring.beans.spel.Car" >
< property name = " brand" value = " Audi" > </ property>
< property name = " price" value = " 300000" > </ property>
< property name = " tyrePerimeter" value = " #{T(java.lang.Math).PI * 80}" > </ property>
</ bean>
< bean id = " person" class = " com.fafa.spring.beans.spel.Person" >
< 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 = " Tom" > </ property>
</ bean>
测试方法获取bean:@Test
public void testSpel ( ) {
ApplicationContext ctx = new ClassPathXmlApplicationContext ( "classpath*:beans-spel.xml" ) ;
Address address = ( Address) ctx. getBean ( "address" ) ;
System. out. println ( address) ;
Car car = ( Car) ctx. getBean ( "car" ) ;
System. out. println ( car) ;
Person person = ( Person) ctx. getBean ( "person" ) ;
System. out. println ( person) ;
}
测试结果:Address [ city= BeiJing, street= WuDaoKou]
Car [ brand= Audi, price= 300000.0 , tyrePerimeter= 251.32741228718345 ]
Person [ name= Tom, city= BeiJing, info= 白领, car= Car [ brand= Audi, price= 300000.0 , tyrePerimeter= 251.32741228718345 ] ]