Spring EL表达式共有以下几种语法知识:
使用SpEL引用bean使用SpEL调用方法SpEL的运算符SpEL的三元运算SpEL操作集合类型SpEL的正则表达式Spring EL表达式有两种实现方式:
基于xml方式的实现基于注解方式的实现在下面的每一个知识点中,均会以两种形式来介绍
SpEL 的入门例子
第一步:先创建两个实体类 Teacher.java
public class Teacher { private Student student; private String studentName; //setter and getter methods //toString methods}Student.java
public class Student { private String name; private String type; //setter and getter methods //toString methods}1、以xml方式实现
bean.xml
<bean id="Student" class="com.main.autowrite.EL.Student"> <property name="name" value="jack"/> <property name="type" value="student"/> </bean> <bean id="Teacher" class="com.main.autowrite.EL.Teacher"> <property name="student" value="#{Student}"/> <property name="name" value="#{Student.name}"/> </bean>测试代码和运行结果:
@Test public void test(){ applicationContext context = new ClassPathXmlApplicationContext("com/main/autowrite/EL/bean.xml"); Teacher teacher = (Teacher)context.getBean("Teacher"); System.out.println(teacher.toString()); }2、以注解方式实现
修改你的两个实体类如下: Teacher.java:
@Component("Teacher")public class Teacher { @Value("#{Student}") private Student student; @Value("#{Student.name}") private String studentName;}Student.java
@Component("Student")public class Student { @Value("jack") private String name; @Value("student") private String type;}然后在bean配置文件中启用组件扫描
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.main.autowrite.EL" /></beans>最后测试运行:
@Test public void test(){ ApplicationContext context = new ClassPathXmlApplicationContext("com/main/autowrite/EL/bean.xml"); Teacher teacher = (Teacher)context.getBean("Teacher"); System.out.println(teacher.toString()); }至此,SpEL的入门例子就完成了。 接下来学习SpEL的其他语法知识。
1、使用SpEL引用bean
以注解的形式引用:
Teacher.java
@Component("Teacher")public class Teacher { @Value("#{Student}") private Student student; @Value("#{Student.name}") private String studentName;}Student.java
@Component("Student")public class Student { @Value("jack") private String name; @Value("student") private String type;}以XML的形式引用:
<context:component-scan base-package="com.main.autowrite.EL" /> <bean id="Student" class="..."> <property name="name" value="jack"/> </bean> <bean id="Teacher" class="..."> <property name="Student" value="#{Student}"/> <property name="studentName" value="#{Student.name}"/> </bean>2、使用SpEL调用方法
以注解的形式调用:
@Component("Teacher")public class Teacher { @Value("#{Student}") private Student student; @Value("#{Student.getName()}") private String studentName;}@Component("Student")public class Student { @Value("jack") private String name; @Value("student") private String type;}以XML的形式调用:
<context:component-scan base-package="com.main.autowrite.EL" /> <bean id="Student" class="..."> <property name="name" value="jack"/> </bean> <bean id="Teacher" class="..."> <property name="Student" value="#{Student}"/> <property name="studentName" value="#{Student.getName()}"/> </bean>3、SpEL的运算符
说明:这里说得运算符是指二目运算符
一目:作用预单个运算数,a++,b–等;++和–就是单目运算符 二目:作用于两个运算数,a+b,a*c等。常用的+,-,*,/就是二目运算符 三目:所用于三个运算数,a>b?true:false等。
在注解中使用运算符
@Component("Customer")public class Customer{ @Value("#{10==1}") //false private boolean Value; @Value("#{10-1}")//9 private int intNumber; @Value("#{10/2}") //5.0 private double doubleNumber; //....... //setter and getter methods}在XML中使用运算符
<bean id="customerBean" class="..."> <property name="testEqual" value="#{1 == 1}" /> <property name="testNotEqual" value="#{1 != 1}" /> <property name="testLessThan" value="#{1 lt 1}" /> <property name="testLessThanOrEqual" value="#{1 le 1}" /> <property name="testGreaterThan" value="#{1 > 1}" /> <property name="testGreaterThanOrEqual" value="#{1 >= 1}" /> <property name="testAnd" value="#{numberBean.no == 999 and numberBean.no lt 900}" /> <property name="testOr" value="#{numberBean.no == 999 or numberBean.no lt 900}" /> <property name="testNot" value="#{!(numberBean.no == 999)}" /> <property name="testAdd" value="#{1 + 1}" /> <property name="testAddString" value="#{'1' + '@' + '1'}" /> <property name="testSubtraction" value="#{1 - 1}" /> <property name="testMultiplication" value="#{1 * 1}" /> <property name="testDivision" value="#{10 / 2}" /> <property name="testModulus" value="#{10 % 10}" /> <property name="testExponentialPower" value="#{2 ^ 2}" /> </bean> <bean id="numberBean" class="..."> <property name="no" value="999" /> </bean>4、SpEL的三目运算符
注解形式的三目运算符
@Value("#{10 < 100 ? true : false}") //true private boolean warning;XML形式的三目运算符
<property name="number" value="#{10 < 100 ? 10 : 100}"/>5、SpEL的集合
注解形式的集合
@Component("testBean")public class Test { private Map<String, String> map; private List<String> list;}假设Map和List集合均已经存在初始值
//引用 @Value("#{testBean.map['MapA']}") private String mapA; @Value("#{testBean.list[0]}") private String list;XML形式的集合
<bean id="customerBean" class="..."> <property name="mapA" value="#{testBean.map['MapA']}" /> <property name="list" value="#{testBean.list[0]}" /> </bean>6、SpEL的正则表达式
注解形式的正则表达式:
// 校验Email的正则表达式 String emailRegEx = "^[_A-Za-z0-9-]+(//.[_A-Za-z0-9-]+)" + "*@[A-Za-z0-9]+(//.[A-Za-z0-9]+)*(//.[A-Za-z]{2,})$"; // true @Value("#{'100' matches '//d+' }") private boolean validDigit; // msg:yes this is digit @Value("#{ ('100' matches '//d+') == true ? " + "'yes this is digit' : 'No this is not a digit' }") private String msg;XML形式的正则表达式:
<property name="message" value="#{ ('100' matches '/d+') == true ? 'yes this is digit' : 'No this is not a digit' }" /> <property name="validateEmail" value="#{emailBean.emailAddress matches '^[_A-Za-z0-9-]+(/.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(/.[A-Za-z0-9]+)*(/.[A-Za-z]{2,})$' }" />新闻热点
疑难解答