语法上类似于EL:SpEL使用#{...}作为界定符,所有在大框号中的字符都将被认为是SpEL,SpEL为bean的属性进行动态赋值提供了便利。
SpEL有以下几种用法:
1)引用字面量的值
4)调用方法以及引用对象中的属性
5)计算表达式的值
6)正则表达式的匹配
下面我们就来实现以上的几种用法,首先我们先创建几个实体类:
Person类
1 public class Person { 2 private String name; 3 private Car car; 4 //应用address bean的city属性 5 private String city; 6 //根据car的price确定info,car的price>300000 金领 否则白领 7 8 public String getInfo() { 9 return info; 10 } 11 12 public void setInfo(String info) { 13 this.info = info; 14 } 15 16 private String info; 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public Car getCar() { 27 return car; 28 } 29 30 public void setCar(Car car) { 31 this.car = car; 32 } 33 34 @Override 35 public String toString() { 36 return "Person{" + 37 "name=‘" + name + ‘\‘‘ + 38 ", car=" + car + 39 ", city=‘" + city + ‘\‘‘ + 40 ", info=‘" + info + ‘\‘‘ + 41 ‘}‘; 42 } 43 44 public String getCity() { 45 return city; 46 } 47 48 public void setCity(String city) { 49 this.city = city; 50 } 51 }
Car类
1 public class Car { 2 private String brand; 3 private double price; 4 //轮胎的周长 5 private double tyrePerimeter; 6 7 @Override 8 public String toString() { 9 return "Car{" + 10 "brand=‘" + brand + ‘\‘‘ + 11 ", price=" + price + 12 ", tyrePerimeter=" + tyrePerimeter + 13 ‘}‘; 14 } 15 16 public String getBrand() { 17 return brand; 18 } 19 20 public void setBrand(String brand) { 21 this.brand = brand; 22 } 23 24 public double getPrice() { 25 return price; 26 } 27 28 public void setPrice(double price) { 29 this.price = price; 30 } 31 32 public Car() { 33 System.out.println("Car‘s Constructor"); 34 } 35 36 public double getTyrePerimeter() { 37 return tyrePerimeter; 38 } 39 40 public void setTyrePerimeter(double tyrePerimeter) { 41 this.tyrePerimeter = tyrePerimeter; 42 } 43 }
Address类
1 public class Person { 2 private String name; 3 private Car car; 4 //应用address bean的city属性 5 private String city; 6 //根据car的price确定info,car的price>300000 金领 否则白领 7 8 public String getInfo() { 9 return info; 10 } 11 12 public void setInfo(String info) { 13 this.info = info; 14 } 15 16 private String info; 17 18 public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public Car getCar() { 27 return car; 28 } 29 30 public void setCar(Car car) { 31 this.car = car; 32 } 33 34 @Override 35 public String toString() { 36 return "Person{" + 37 "name=‘" + name + ‘\‘‘ + 38 ", car=" + car + 39 ", city=‘" + city + ‘\‘‘ + 40 ", info=‘" + info + ‘\‘‘ + 41 ‘}‘; 42 } 43 44 public String getCity() { 45 return city; 46 } 47 48 public void setCity(String city) { 49 this.city = city; 50 } 51 }
1)引用字面量
可以直接通过#{‘字面量的值’}方式对bean的属性进行注入,但是这种方式本身意义不大,因为在value属性中直接填写value的值即可。
1 <bean id="address" class="com.wzy.spel.Address"> 2 <!--使用spel为属性赋值一个字面值--> 3 <property name="city" value="#{‘BeiJing‘}"/> 4 <property name="street" value="WUDAOKOU"/> 5 </bean>
2)引用类的静态属性与计算表达式的值
声明一个Car类型的bean,并且应用Math类中的今天变量PI,来计算汽车轮胎的周长
1 <bean id="car" class="com.wzy.spel.Car"> 2 <property name="brand" value="Audi"/> 3 <property name="price" value="300000"/> 4 <!--使用spel 引用类的静态属性--> 5 <property name="tyrePerimeter" value="#{T(java.lang.Math).PI * 80}"/> 6 </bean>
3)引用其他的bean
声明一个Person类型的Bean,并使用SpEL表达式注入属性。
1 <bean id="person" class="com.wzy.spel.Person"> 2 <!--使用spEL来引用其他的Bean--> 3 <property name="car" value="#{car}"/> 4 <!--使用spEl引用其他Bean的属性--> 5 <property name="city" value="#{address.city}"/> 6 <property name="name" value="wzy"/> 7 <!--在spEL中使用运算符,如果车的价格大于三十万则说明是金领,否则是白领--> 8 <property name="info" value="#{car.price > 300000 ? ‘金领‘ : ‘白领‘}"/> 9 </bean>
编写测试类:
1 public class Main { 2 public static void main(String[] args) { 3 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml"); 4 //获取address对象 5 Address address = (Address) ctx.getBean("address"); 6 System.out.println(address); 7 //获取car对象 8 Car car = (Car) ctx.getBean("car"); 9 System.out.println(car); 10 //获取person对象 11 Person person = (Person) ctx.getBean("person"); 12 System.out.println(person); 13 } 14 }
执行结果:
原文:https://www.cnblogs.com/fengyun2019/p/10836498.html