通过Bean的id对Bean进行引用 调用方法及引用对象的属性 计算表达式的值 正则表达式匹配
字面量的表示:
整型:#{5} 小数:#{3.45} 科学计数法:#[1e4] 字符串:可以使用单引号或双引号早味字符串的定界符:#{'tom'},#{"tom"} 布尔值:#{false}
SpEL支持的运算符号:+ - * / % ^ < > == <= >= lt gt eq le ge and or not | (?:) if-else 正则表达式等等
调用静态属性和方法:通过T()
以下代码均忽略类中的getter、setter和to_string方法。
Car.java
package com.gong.spring.beans.spel;public class Car { public Car() { } public Car(String name) { this.name = name; } private String name; //轮胎周长 private double tyrePerimeter; private double price;}
Address.java
package com.gong.spring.beans.spel;public class Address { private String city; private String street;}
Student.java
package com.gong.spring.beans.spel;public class Student { private String name; private int age; private double score; private String city; private String info; private Car car; private Address address;}
beans-spel.xml
Main.java
package com.gong.spring.beans.spel;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main { public static void main(String[] args) { //1.创建spring的IOC容器对象 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml"); //2.从容器中获取Bean实例 Student student = (Student) ctx.getBean("student"); System.out.println(student.toString()); }}