(1) spring expression language is a support query and manipulate an object graph I have a strong expression language runtime.
(2) language similar EL: SpEL using # {...} as delimiters. All strings in braces are considered SpEL.
(3) SpEL dynamically assigned to the bean property provides convenient.
(4) can be achieved by SpEL:
- Make reference to Bean by Bean's id
- Call methods and properties referenced object
- Value calculation expression
- Regular expression match
Literal representation:
- Integer: # {5}
- Decimal: # {3.45}
- Scientific notation: # [1e4]
- String: may be used single or double quotes delimiter early taste string: # { 'tom'}, # { "tom"}
- Boolean: # {false}
SpEL support operation symbol: + - * /% ^ <> == <=> = lt gt eq le ge and or not | (:) if-else like a regular expression?
Call static properties and methods: by T ()
The following codes are ignored class getter, setter and to_string method.
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
<?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"> <bean id="car" class="com.gong.spring.beans.spel.Car" p:name="baoma"
p:tyrePerimeter="#{T(java.lang.Math).PI*80}"></bean> <bean id="address" class="com.gong.spring.beans.spel.Address" p:city="武汉" p:street="#{'络南街道'}"></bean> <bean id="student" class="com.gong.spring.beans.spel.Student" p:name="tom" p:age="#{12}" p:score="#{99.00}"> <!-- 使用spel引用其它的bean --> <property name="car" value="#{car}"></property> <!- nameProperty<->Use spel reference other attributes bean = "City" value = "# {} address.city" > </ Property > <-! Use spel operator, dynamic evaluation -> < Property name = "info" value = "# {car.price> ? 300000 'rich': 'money'} " > </ Property > </ the bean > </ Beans >
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()); } }
Part of the output: