- spel :spring表达式语言 简称(spel)是一个支持运行时查询和操作对象图的强大表达式语言
- 语法类似el :spel使用 #{...}作为定界符,所有在大括号中的字符都将被认为是spel
- spel 为bean的属性进行动态赋值提供了便利
通过spel可以实现
a.通过bean‘的id对bean进行引用
b.调用方法以及引用对象中的属性
c.计算表达式的值
d.正则表达式的匹配
下面做个小测试
4. 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="applic" class="com.spring.entity.applic"> <property name="address" value="北京"></property> <property name="age" value="14"></property> <property name="name" value="王府"></property> <property name="sex" value="F"></property> </bean> <bean id="spel" class="com.spring.entity.Spel"> <property name="price" value="#{33}"></property> <!-- 使用spel引用其他的 bean --> <property name="applic" value="#{applic}"></property> <!-- 引用其他bean的属性 --> <property name="name" value="#{applic.name}"></property> <!-- 引用其他bean的方法 --> <property name="addr" value="#{applic.te()}"></property> </bean> </beans>
5.applic 实体
package com.spring.entity; public class applic { private String name; private String address; private Integer age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "applic [name=" + name + ", address=" + address + ", age=" + age + ", sex=" + sex + "]"; } public applic(String name, String address, Integer age, String sex) { super(); this.name = name; this.address = address; this.age = age; this.sex = sex; } public applic() { super(); } public void te(){ System.out.println("我是applic的te()方法!!!"); } }
6.spel 实体
package com.spring.entity; public class Spel { private String name; private String addr; private String price; private applic applic; public applic getApplic() { return applic; } public void setApplic(applic applic) { this.applic = applic; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public Spel(String name, String addr, String price, com.spring.entity.applic applic) { super(); this.name = name; this.addr = addr; this.price = price; this.applic = applic; } public Spel() { super(); } @Override public String toString() { return "Spel [name=" + name + ", addr=" + addr + ", price=" + price + ", applic=" + applic + "]"; } }
7.测试
package com.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.entity.Spel; public class TestSpel { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("spel.xml"); Spel spel = (Spel)ctx.getBean("spel"); System.out.println(spel); } }
测试结果:
我是applic的te()方法!!!
Spel [name=王府, addr=null, price=33, applic=applic [name=王府, address=北京, age=14, sex=F]]
时间: 2024-10-21 12:52:22