实现步骤
- 需要导入jar包 :spring-expression.jar
- 解析字符串
SpringEL核心接口
- ExpressionParser: 解析表达式字符串,表达式字符串是一个用单引号标注或者用转义的双引号标注的字符串.
- Expression: 接口用来计算表达式字符串值
测试
导入jar包
spring-expression.jar
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
1.springel解析普通类型值
@Test
public void test1(){
//创建表达解析器
// 进行解析表达式 的到结果
ExpressionParser ep=new SpelExpressionParser();
double num1=(Double)ep.parseExpression("3.123").getValue();
System.out.println(num1);
}
2.解析方法
@Test
public void tes2(){
//创建解析器
//通过表达式解析得到结果
ExpressionParser ep=new SpelExpressionParser();
String s=(String) ep.parseExpression("'abc'.concat('a')").getValue();
System.out.println(s);
}
@Test
public void test3(){
//创建解析器
//通过表达式解析得到结果
ExpressionParser ep=new SpelExpressionParser();
boolean b=(Boolean) ep.parseExpression("'abc'.equals('a')").getValue();
System.out.println(b);
}
解析数组
@Test
public void test4(){
ExpressionParser ep=new SpelExpressionParser();
String[] s=(String[]) ep.parseExpression("new String[]{'a','b'}").getValue();
System.out.println(Arrays.toString(s));
}
解析List集合
@Test
public void test5(){
ExpressionParser ep=new SpelExpressionParser();
List list=(List) ep.parseExpression("{1,2,3,4,'hah'}").getValue();
for (Object o:list){
System.out.println(o);
}
}
解析Map集合
@Test
public void test6(){
ExpressionParser ep=new SpelExpressionParser();
Map m=(Map) ep.parseExpression("{'hah':1,'age':'heihei'}").getValue();
System.out.println(m.get("hah"));
System.out.println(m.get("age"));
}
解析对象类型,修改对象值
@Test
public void test7(){
//创建对象
//创建解析器,
// EvaluationContext 评估上下文, 绑定对象
//通过解析表达式获得结果
//Standard 标准 Evaluation评估
Grade g=new Grade("二年级");
Users u=new Users("hah",g);
ExpressionParser ep=new SpelExpressionParser();
EvaluationContext ec=new StandardEvaluationContext(u);
String username=(String)ep.parseExpression("name").getValue(ec);
String gradename=(String)ep.parseExpression("grade.name").getValue(ec);
String username1=(String)ep.parseExpression("name").getValue(u);
System.out.println("ec"+username);
System.out.println("ec"+gradename);
System.out.println("u,,,,"+username1);
//修改对象值
ep.parseExpression("name").setValue(ec,"wanger");
String name1=(String) ep.parseExpression("name").getValue(ec);
System.out.println("修改后的"+name1);
}
运算符解析
@Test
public void test8(){
ExpressionParser ep=new SpelExpressionParser();
boolean br1=(Boolean)ep.parseExpression("2==4").getValue();
System.out.println("br"+br1);
String br2= (String) ep.parseExpression("2>3?'a':'b'").getValue();
System.out.println("br3="+br2);
}
构造方法解析
@Test
//无参构造
public void test9(){
ExpressionParser ep=new SpelExpressionParser();
Users u=(Users)ep.parseExpression("new com.fdk.been.Users()").getValue();
System.out.println(u);
}
@Test
//有参构造
public void test10(){
ExpressionParser ep=new SpelExpressionParser();
Users u=(Users)ep.parseExpression("new com.fdk.been.Users('hah')").getValue();
System.out.println(u);
}
调用类中方法
public void test11(){
Users u=new Users();
EvaluationContext ec=new StandardEvaluationContext(u);
ExpressionParser ep=new SpelExpressionParser();
//非静态,无返回值和参数
ep.parseExpression("abc()").getValue(ec);
//调取静态方法,有参,有返回值
String s=(String)ep.parseExpression("bcd('aaaa')").getValue(ec);
System.out.println("静态方法返回值"+s);
}
.xml文件引入其他工具类
T( );
<bean id="xx" name="xx" class="xx.xx.xx" p:age="#{T(java.lang.Math).random()*100 }"/>