主要就是使用#{xxx}表达式来动态引用数据或者输入数据等等
- bean-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">
<!--
Spring表达式语言:SpEL
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。
语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL
SpEL 为 bean 的属性进行动态赋值提供了便利
通过 SpEL 可以实现:
通过 bean 的 id 对 bean 进行引用
调用方法以及引用对象中的属性
计算表达式的值
正则表达式的匹配
-->
<!--
字面量的表示:
整数:<property name="count" value="#{5}"/>
小数:<property name="frequency" value="#{89.7}"/>
科学计数法:<property name="capacity" value="#{1e4}"/>
String可以使用单引号或者双引号作为字符串的定界符号:<property name=“name” value="#{'Chuck'}"/> 或 <property name='name' value='#{"Chuck"}'/>
Boolean:<property name="enabled" value="#{false}"/>
-->
<bean id="person" class="com.hgh.spring.spel.Person" p:name2="#{'hgh'}" p:age="#{12}" p:city="#{'shangsi'}"></bean>
<!--
SpEL:引用 Bean、属性和方法(1)
引用其他对象:
引用其他对象的属性
调用其他方法,还可以链式操作
-->
<bean id="person2" class="com.hgh.spring.spel.Person">
<property name="age" value="#{person.age}"></property>
<property name="name2" value="#{person.getName()}"></property>
<property name="city" value="#{person.getCity().toUpperCase()}"></property>
</bean>
<!--
SpEL支持的运算符号
算数运算符:+, -, *, /, %, ^:
加号还可以用作字符串连接:
比较运算符: <, >, ==, <=, >=, lt, gt, eq, le, ge
逻辑运算符号: and, or, not, |
if-else 运算符:?: (ternary), ?: (Elvis)
正则表达式:matches
-->
<bean id="person3" class="com.hgh.spring.spel.Person" >
<property name="age" value="#{person.age+13}"></property>
<property name="name2" value="#{person.getName()+''+'dstdawdw'}"></property>
<property name="city" value="#{person.getCity().toUpperCase()}"></property>
</bean>
<bean id="person4" class="com.hgh.spring.spel.Person" >
<property name="age" value="#{person.age+13>30?10:20}"></property>
<property name="name2" value="#{person.getName()+''+'dstdawdw'}"></property>
<property name="city" value="#{person.getCity().toUpperCase()}"></property>
</bean>
<bean id="car" class="com.hgh.spring.spel.Car">
<property name="company" value="bmw"></property>
<property name="price" value="1200"></property>
<property name="maxSpice" value="#{T(java.lang.Math).PI*30}"></property>
</bean>
</beans>
- TestSpring
package com.hgh.spring.spel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-spel.xml");
//从容器中获取Bean
Person person = (Person) ac.getBean("person");
System.out.println(person);
Person person2 = (Person) ac.getBean("person2");
System.out.println(person2);
Person person3 = (Person) ac.getBean("person3");
System.out.println(person3);
Person person4 = (Person) ac.getBean("person4");
System.out.println(person4);
Car car = (Car) ac.getBean("car");
System.out.println(car);
}
}