Spring實(shí)戰(zhàn)之使用Expression接口進(jìn)行表達(dá)式求值操作示例
本文實(shí)例講述了Spring使用Expression接口進(jìn)行表達(dá)式求值操作。分享給大家供大家參考,具體如下:
一 Bean
package org.crazyit.app.domain;
import java.util.Date;
public class Person
{
private Integer id;
private String name;
private Date birth;
// 無(wú)參數(shù)的構(gòu)造器
public Person()
{
}
// 初始化全部成員變量的構(gòu)造器
public Person(Integer id , String name , Date birth)
{
this.id = id;
this.name = name;
this.birth = birth;
}
// id的setter和getter方法
public void setId(Integer id)
{
this.id = id;
}
public Integer getId()
{
return this.id;
}
// name的setter和getter方法
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
// birth的setter和getter方法
public void setBirth(Date birth)
{
this.birth = birth;
}
public Date getBirth()
{
return this.birth;
}
}
二 測(cè)試類(lèi)
package lee;
import org.springframework.expression.*;
import org.springframework.expression.spel.standard.*;
import org.springframework.expression.spel.support.*;
import java.util.*;
import org.crazyit.app.domain.*;
public class SpELTest
{
public static void main(String[] args)
{
// 創(chuàng)建一個(gè)ExpressionParser對(duì)象,用于解析表達(dá)式
ExpressionParser parser = new SpelExpressionParser();
// 最簡(jiǎn)單的字符串表達(dá)式
Expression exp = parser.parseExpression("'HelloWorld'");
System.out.println("'HelloWorld'的結(jié)果: " + exp.getValue());
// 調(diào)用方法的表達(dá)式
exp = parser.parseExpression("'HelloWorld'.concat('!')");
System.out.println("'HelloWorld'.concat('!')的結(jié)果: "
+ exp.getValue());
// 調(diào)用對(duì)象的getter方法
exp = parser.parseExpression("'HelloWorld'.bytes");
System.out.println("'HelloWorld'.bytes的結(jié)果: "
+ exp.getValue());
// 訪問(wèn)對(duì)象的屬性(d相當(dāng)于HelloWorld.getBytes().length)
exp = parser.parseExpression("'HelloWorld'.bytes.length");
System.out.println("'HelloWorld'.bytes.length的結(jié)果:"
+ exp.getValue());
// 使用構(gòu)造器來(lái)創(chuàng)建對(duì)象
exp = parser.parseExpression("new String('helloworld')"
+ ".toUpperCase()");
System.out.println("new String('helloworld')"
+ ".toUpperCase()的結(jié)果是: "
+ exp.getValue(String.class));
Person person = new Person(1 , "孫悟空", new Date());
exp = parser.parseExpression("name");
// 以指定對(duì)象作為root來(lái)計(jì)算表達(dá)式的值
// 相當(dāng)于調(diào)用person.name表達(dá)式的值
System.out.println("以persn為root,name表達(dá)式的值是: "
+ exp.getValue(person , String.class));
exp = parser.parseExpression("name=='孫悟空'");
StandardEvaluationContext ctx = new StandardEvaluationContext();
// 將person設(shè)為Context的root對(duì)象
ctx.setRootObject(person);
// 以指定Context來(lái)計(jì)算表達(dá)式的值
System.out.println(exp.getValue(ctx , Boolean.class));
List<Boolean> list = new ArrayList<Boolean>();
list.add(true);
EvaluationContext ctx2 = new StandardEvaluationContext();
// 將list設(shè)置成EvaluationContext的一個(gè)變量
ctx2.setVariable("list" , list);
// 修改list變量的第一個(gè)元素的值
parser.parseExpression("#list[0]").setValue(ctx2 , "false");
// list集合的第一個(gè)元素被改變
System.out.println("list集合的第一個(gè)元素為:"
+ parser.parseExpression("#list[0]").getValue(ctx2));
}
}
三 測(cè)試結(jié)果
'HelloWorld'的結(jié)果: HelloWorld
'HelloWorld'.concat('!')的結(jié)果: HelloWorld!
'HelloWorld'.bytes的結(jié)果: [B@14899482
'HelloWorld'.bytes.length的結(jié)果:10
new String('helloworld').toUpperCase()的結(jié)果是: HELLOWORLD
以persn為root,name表達(dá)式的值是: 孫悟空
true
list集合的第一個(gè)元素為:false
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring的@Scheduled 如何動(dòng)態(tài)更新cron表達(dá)式
- Spring切入點(diǎn)表達(dá)式配置過(guò)程圖解
- Spring AOP中使用args表達(dá)式的方法示例
- Spring表達(dá)式語(yǔ)言SpEL用法詳解
- Spring與Shiro整合及加載權(quán)限表達(dá)式問(wèn)題
- Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作示例
- 使用Spring安全表達(dá)式控制系統(tǒng)功能訪問(wèn)權(quán)限問(wèn)題
- spring aop execution表達(dá)式的用法
相關(guān)文章
解決javaWEB中前后臺(tái)中文亂碼問(wèn)題的3種方法
這篇文章主要介紹了解決javaWEB中前后臺(tái)中文亂碼問(wèn)題的3種方法,中文問(wèn)題一直是很多人難以解決的問(wèn)題,對(duì)這方面感興趣的朋友可以參考一下2015-11-11
Java 在線考試云平臺(tái)的實(shí)現(xiàn)
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+vue+springboot+mysql+maven實(shí)現(xiàn)一個(gè)前端vue后臺(tái)java微服務(wù)的在線考試系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11

