解析SpEL表达式之前:
ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("需要解析的SpEL表达式");
exp.getValue() //得到解析后的表达式的值
1、算术运算、关系运算的解析
Expression exp=parser.parseExpression("1 eq 10"); //eq(等于)也可以为 ne(不等于) gt(大于) lt(小于) ==(相等) !=(不相等)
System.out.println(exp.getValue()); //输出相对应的true或false
Expression exp=parser.parseExpression("1 +10"); //+也可以为- (减) *(乘) (/) 除
System.out.println(exp.getValue()); //输出相对应的计算结果
2、字符串相关操作解析
Expression exp=parser.parseExpression("'MRW' + ' is' + ' good' + ' people'"); //输出MRW is good people
Expression exp=parser.parseExpression("'Hello World'.concat('!')"); //输出Hello World!
Expression exp=parser.parseExpression("'my name is mrw!'.toUpperCase()"); //输出MY NAME IS MRW!
Expression exp=parser.parseExpression("'mrw'=='mrw'"); //输出true
Expression exp=parser.parseExpression("'life is beautiful'.substring(0,3)"); //输出lif
3、对类中的相关变量进行解析
Family.java
public class Family {
private String sisters;
private String brothers;
public Family(String sisters, String brothers) {
super();
this.sisters = sisters;
this.brothers = brothers;
}
//此处省略get、set方法
}
获取类中的值:
Test1.java
import org.junit.Test;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class Test1 {
@Test
public void test() {
Family family=new Family("姐妹","兄弟");
ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("sisters");
EvaluationContext context = new StandardEvaluationContext(family);
System.out.println(exp.getValue(context));
}
}
运行结果:
对类中变量进行重新赋值:
Family family=new Family("妹妹","哥哥");
ExpressionParser parser=new SpelExpressionParser();
StandardEvaluationContext context=new StandardEvaluationContext(family);
context.setVariable("newBrothers","大哥哥");
parser.parseExpression("brothers=#newBrothers").getValue(context);
System.out.println(family.getBrothers());
运行结果:
通过表达式初始化类:
ExpressionParser parser=new SpelExpressionParser();
Family family=parser.parseExpression("new com.mrw.springtest7.Family('哥哥','妹妹')").getValue(Family.class); //不管该测试类与Family是否在同一包下包名必须要加上
System.out.println(family.getSisters()+" "+family.getBrothers());
运行结果:
4、对数组、List进行解析与及初始化
对List进行初始化:
ExpressionParser parser=new SpelExpressionParser();
List numbers=(List)parser.parseExpression("{1,2,3,4}").getValue();
for(Object object:numbers){
System.out.print(object+" ");
}
运行结果:
对List进行解析:
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class Test1 {
public List<String> booleanList=new ArrayList<String>();
@Test
public void test() {
booleanList.add("MRW");
StandardEvaluationContext simpleContext=new StandardEvaluationContext(this);
simpleContext.setVariable("booleanList",booleanList);
ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("booleanList[0]");
System.out.println(exp.getValue(simpleContext));
}
}
注意:
- booleanList访问权限必须为public
- public List<String> booleanList=new ArrayList<String>()不能放在test方法内
- 不能像如下这样写:
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class Test1 {
@Test
public void test() {
booleanList.add("MRW");
StandardEvaluationContext simpleContext=new StandardEvaluationContext(this);
simpleContext.setVariable("booleanList",booleanList);
ExpressionParser parser=new SpelExpressionParser();
Expression exp=parser.parseExpression("booleanList[0]");
System.out.println(exp.getValue(simpleContext));
}
}
这样是无法被识别的
运行结果:
对数组进行初始化:
ExpressionParser parser=new SpelExpressionParser();
int[ ] number=(int[ ])parser.parseExpression("new int[ ]{11,12,13}").getValue();//new int[]{11,12,13}如果换成{11,12,13}将无法初始化为int[ ]
for(int j:number){
System.out.println(j+" ");
}
运行结果:
5、对解析正则表达式
ExpressionParser parser=new SpelExpressionParser();
boolean trueValue = parser.parseExpression("'15974577473' matches '15[0-9]{9}'").getValue(Boolean.class); //判断是否是一个以15开头的电话号码
System.out.println(trueValue);
6、Spring配置文件中的使用
Computer.java
package com.mrw.springtest8;
public class Computer {
private String brand;
private double price;
//此处省略get、set方法
}
Life.java
package com.mrw.springtest8;
public class Life {
private String equipment;
private double price;
private String computername;
private double computerprice;
//此处省略get、set方法
}
beans.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"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<bean id="life" class="com.mrw.springtest8.Life">
<property name="equipment" value="#{'电视机'}"></property>
<property name="price" value="#{T(java.lang.Math).random()*2500}"></property>
<property name="computername" value="#{computer.brand}"></property>
<property name="computerprice" value="#{computer.price}"></property>
</bean>
<bean id="computer" class="com.mrw.springtest8.Computer">
<property name="brand" value="#{'华硕'}"></property>
<property name="price" value="#{3500}"></property>
</bean>
</beans>
注意:在配置文件中SpEL的格式为#{书写的内容},去掉#将无法得到相应的效果
Test1.java
package com.mrw.springtest8;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
@Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Life life=(Life)context.getBean("life");
System.out.println("电器的名称为:"+life.getEquipment()+"电器幸运价格为:"+life.getPrice()+"电脑的名字为:"+life.getComputername()+"电脑的价格为:"+life.getComputerprice());
}
}
运行结果为: