Spring SpEL表达式语言
2017-05-24 21:34
423 查看
Spring SpEL表达式语言
一、说在前面
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL。SpEL 为 bean 的属性进行动态赋值提供了便利。
通过 SpEL 可以实现:
(1)通过 bean 的 id 对 bean 进行引用;
(2)调用方法以及引用对象中的属性;
(3)计算表达式的值;
(4)正则表达式的匹配。
二、举例说明
1、Address、Car和Person类
Address类:
2、配置文件
3、测试函数
4、测试结果
By luoyepiaoxue2014
微博地址:http://weibo.com/luoyepiaoxue2014 点击打开链接
一、说在前面
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。语法类似于 EL:SpEL 使用 #{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL。SpEL 为 bean 的属性进行动态赋值提供了便利。
通过 SpEL 可以实现:
(1)通过 bean 的 id 对 bean 进行引用;
(2)调用方法以及引用对象中的属性;
(3)计算表达式的值;
(4)正则表达式的匹配。
二、举例说明
1、Address、Car和Person类
Address类:
package com.at.beans.spel; public class Address { private String city; private String street; public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } @Override public String toString() { return "Address [city=" + city + ", street=" + street + "]"; } }Car类
package com.at.beans.spel; public class Car { private String brand; private double price; //轮胎周长 private double ltzhouchang; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public double getLtzhouchang() { return ltzhouchang; } public void setLtzhouchang(double ltzhouchang) { this.ltzhouchang = ltzhouchang; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + ", ltzhouchang=" + ltzhouchang + "]"; } }Person类
package com.at.beans.spel; public class Person { private String name; private Car car; //引用address bean的city属性 private String city; //根据car的price属性来确定info:car的price>400000为金领,否则为白领 private String info; public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getInfo() { return info; } public void setInfo(String info) { this.info = info; } @Override public String toString() { return "Person [name=" + name + ", car=" + car + ", city=" + city + ", info=" + info + "]"; } }
2、配置文件
<?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="address" class="com.at.beans.spel.Address"> <!-- 使用SpEL为属性赋一个字面值 --> <property name="city" value="#{'BeiJing'}"></property> <property name="street" value="huoying"></property> </bean> <bean id="car" class="com.at.beans.spel.Car"> <property name="brand" value="Audi"></property> <property name="price" value="300000"></property> <!-- 使用SpEL引用类的静态属性 --> <property name="ltzhouchang" value="#{T(java.lang.Math).PI * 50}"></property> </bean> <bean id="person" class="com.at.beans.spel.Person"> <!-- 使用SpEL来应用其他的bean --> <property name="car" value="#{car}"></property> <!-- 使用SpEL来应用其他的bean的属性 --> <property name="city" value="#{address.city}"></property> <!-- 使用SpEL中使用运算符 --> <property name="info" value="#{car.price > 400000 ? '金领':'白领'}"></property> <property name="name" value="luoyepiaoxue2014"></property> </bean> </beans>
3、测试函数
package com.at.beans.spel; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestSpel { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-spel.xml"); Address address = (Address) ctx.getBean("address"); System.out.println(address); Car car = (Car) ctx.getBean("car"); System.out.println(car); Person person = (Person) ctx.getBean("person"); System.out.println(person); } }
4、测试结果
五月 24, 2017 9:40:45 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@427a39ea: startup date [Wed May 24 21:40:45 CST 2017]; root of context hierarchy 五月 24, 2017 9:40:45 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [beans-spel.xml] Address [city=BeiJing, street=huoying] Car [brand=Audi, price=300000.0, ltzhouchang=157.07963267948966] Person [name=luoyepiaoxue2014, car=Car [brand=Audi, price=300000.0, ltzhouchang=157.07963267948966], city=BeiJing, info=白领]
By luoyepiaoxue2014
微博地址:http://weibo.com/luoyepiaoxue2014 点击打开链接
相关文章推荐
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
- Spring3 表达式语言(SpEL)介绍
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- 【Spring学习笔记】之【5.3 Spring表达式语言之SpEL语法】
- 【Spring学习笔记】之【5.2 Spring表达式语言之SpEL基础】
- Spring表达式语言SpEL简介
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- Spring-----12、Spring3.0提供的表达式语言(spel)
- Spring3.0 —— 表达式语言(SpEL)
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- Spring3表达式语言(SpEL)的预定义变量
- Spring 表达式语言 (Spring Expression Language) SpEL
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
- 【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
- 【第五章】Spring表达式语言 之 5.3 SpEL语法 ——跟我学spring3
- Spring 表达式语言 (Spring Expression Language) SpEL
- Spring表达式语言(Spring Expression Language,SpEL)
- Spring3表达式语言(SpEL)学习笔记
- Spring3表达式语言(SpEL)学习笔记