Spring表达式语言:SpEL

简介
Spring表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。语法类似EL:SpEL使用#{…}作为定界符,所有在大括号中的字符都将被认为SpEL。SpEL为bean属性的动态赋值提供了便利。
通过 SpEL 可以实现:
通过 bean 的 id 对 bean 进行引用
调用方法以及引用对象中的属性
计算表达式的值
正则表达式的匹配

1.字面量的赋值

<!--整数-->
<property name="count" value="#{5}"/>
<!--小数-->
<property name="frequency" value="#{1.2}">
<!--科学计数法-->
<property name="capacity" value="#{le4}"/>
<!--String 可以使用单引号或者双引号作为字符串的定界符号-->
<property  name="name" value="#{'Tom'}"/>或者是
<property name="name" value='#{"Tom"}'/>
<!--Boolean-->
<property name="flag" value="#{false}"/>

2.引用其他对象
在这里插入图片描述
3.SpEL支持的运算符号
算数运算符:+,-,*,/,%,^
在这里插入图片描述
加号还可以作为字符串连接

<property  name="infor" value="#{'name= '+'Tome'}"/>

比较运算符

<property name="equal" value="#{couter.total == 100}" />

逻辑运算符:and,or,not,|

<property  name="aaa" value="#{shape.kind =='circle' and shape.price ==1000}"/>
<property name="bb" value="!shape.isAvailable"/>
<property name="cc" value="not shape.isAvailable"/>

if-else运算符: ?…:…

<property name="infor" value="car.price > 300000 ? '金领' : '白领' "/>

调用静态方法或静态属性:通过 T() 调用一个类的静态方法,它将返回一个 Class Object,然后再调用相应的方法或属性:

<property  name="initValue" value="#{T(java.lang.Math).PI }"/>

示例代码:

//准备一个Car类
public class Car {
	private String brand;
	private double price;
	private double perimeter;

	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 getPerimeter() {
		return perimeter;
	}
	public void setPerimeter(double perimeter) {
		this.perimeter = perimeter;
	}
	
	
	
	@Override
	public String toString() {
		return "Car [brand=" + brand + ", price=" + price + ", perimeter=" + perimeter + "]";
	}
	public Car(String brand, double price) {
		super();
		this.brand = brand;
		this.price = price;
	}
	public Car() {}




//准备一个Address类
public class Address {
	private String city;
	private String stree;
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getStree() {
		return stree;
	}
	public void setStree(String stree) {
		this.stree = stree;
	}
	

	@Override
	public String toString() {
		return "Address [city=" + city + ", stree=" + stree + "]";
	}
	public Address(String city, String stree) {
		super();
		this.city = city;
		this.stree = stree;
	}
	
	public Address() {}
	
//准备一个Perosn类
public class Person {
	private String name;
	private Car car;
	private String info;
	private String city;
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	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 getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}

	
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", car=" + car + ", info=" + info + ", city=" + city + "]";
	}
	public Person(String name, Car car, String info) {
		super();
		this.name = name;
		this.car = car;
		this.info = info;
	}
	
	public Person() {}

准备xml文件,配置bean

<?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:util="http://www.springframework.org/schema/util"
	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
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<bean id="car" class="Car的全类名">
<property name="brand" value="#{'audi'}"></property>
<property name="price" value="#{600000}"></property>
<property name="perimeter" value="#{T(java.lang.Math).PI * 80}"></property>
</bean>

<bean id="address" class="Address的全类名">
	<property name="city" value="beijing">
	<property name="stree" vlaue="wudaokou"></property>
</bean>

<property id="person" class="Person的全类名">
<property name="name" value="Tom"></property>
		<property name="car" value="#{car}"></property>
		<property name="city" value="#{address.city}"></property>
		<property name="info" value="#{car.price > 300000 ? '金领' : '白领'}"></property>

</property>


</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值