Spring(2、Spring中Spel的使用)
来源:互联网 发布:linux系统装informix 编辑:程序博客网 时间:2024/04/29 10:57
Spring中Spel的使用
注:本文用到的jar请到Spring(1-1、基于xml装配Bean)中查找
Spring 表达式语言(简称SpEL):是一个支持运行时查询和操作对象图的强大的表达式语言。
语法类似于 EL:SpEL 使用#{…} 作为定界符,所有在大框号中的字符都将被认为是 SpEL
SpEL 为 bean 的属性进行动态赋值提供了便利。
通过 SpEL 可以实现:
通过 bean 的 id 对 bean 进行引用。
调用方法以及引用对象中的属性。
计算表达式的值。
正则表达式的匹配。
例如:
字面量的表示:
整数:<propertyname="count" value="#{5}"/>
小数:<propertyname="frequency" value="#{89.7}"/>
科学计数法:<propertyname="capacity" value="#{1e4}"/>
String可以使用单引号或者双引号作为字符串的定界符号:
<propertyname=“name” value="#{'Chuck'}"/> 或
<propertyname='name' value='#{"Chuck"}'/>
Boolean:<property name="enabled" value="#{false}"/>
javaBean:
public class Address {private String city;private String street;public class Person {private String name;private Address address;private Car car;// 引用address bean的city属性private String city;// 根据car的price确定info:car的price>=300000:金额.否则为白领private String info;public class Car {private String brand;private double price;// 周长private double tyrePerimeter;xml的配置:
<!-- 使用spel为属性配置一个字面值 --><bean id="address" class="org.mybatis.bean.spel.Address"><property name="city" value="#{'BeiJing'}"></property><property name="street" value="wudaokou"></property></bean><bean id="car" class="org.mybatis.bean.spel.Car"><property name="brand" value="Audi"></property><property name="price" value="500000"></property><!-- 使用SPEL引用类的静态属性 --><property name="tyrePerimeter" value="#{T(java.lang.Math).PI*80}"></property></bean><bean id="person" class="org.mybatis.bean.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>300000?'金领':'白领'}"></property><property name="name" value="Tom"></property></bean>
算数运算符:+, -, *, /, %, ^
<property name="adjustedAmount" value="#{counter.total+42}"></property><property name="adjustedAmount" value="#{counter.total-20}"></property><property name="cirumference" value="#{2 * T(java.lang.Math).PI*circle.radius}"></property><property name="average" value="#{counter.total%counter.count}"></property><property name="remainder" value="#{counter.total%counter.count}"></property><property name="area" value="#{T(java.lang.Math).PI*circle.radius^2}"></property>
加号还可以作为字符串连接:
<constructor-arg value="#{performer.firstName+''+performer.lastName}"></constructor-arg>
逻辑运算符:and, or, not, |
<property name="largeCircle"value="shape.kind=='circle' and shape.perimeter gt 10000"></property><property name="outOfStock" value="#{!product.available}"></property><property name="outOfStock" value="#{not product.available}"></property>
if-else变体:
<property name="info" value="#{car.price>300000?'金领':'白领'}"></property>
正则表达式:
<constructor-argvalue="#{admin.email matches'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9._%-]+\\.[a-zA-z]{2,4}'}"></constructor-arg>测试:
@Testpublic void test1() {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);}
阅读全文
1 0
- Spring(2、Spring中Spel的使用)
- 在Spring中使用SpEl
- Spring SpEL语言的使用
- 5、Spring SPEL使用之--在XML中使用SPEL
- spring 使用spel
- Spring的SpEL表达式标准简单使用
- spring 的Spel
- spring的SpEl表达式
- Spring 的 SpEL
- spring SpEL
- spring SpEL
- Spring --- SpEL
- Spring SpEL
- Spring - SpEL
- Spring-SpEL
- spring-SpEL
- Spring---SpEL
- 6、Spring SPEL使用之--在Java类中使用SPEL
- conda的常用命令
- UML用例图的编写
- linux内核入门(1)——基本简介和编译
- Python Socket编程详解
- 网易2017春招实习生笔试编程题集合(C++方向)
- Spring(2、Spring中Spel的使用)
- 【c语言】单链表的基本操作
- c#窗体应用程序 取消输出控制台
- 离散题目11(判双射函数)
- 坚持#第184天~我的梦想是变优秀
- linux查看系统的日志的一些实用操作
- VC++判断硬盘容量,依时间顺序自动删除文件
- 提高代码优雅-Lombok代替Getter/Setter方法
- StringBuild 与 String 进行字符串相加性能比较