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)文章
Java中Array、List、Map相互轉(zhuǎn)換的方法詳解
這篇文章主要介紹了Java中Array、List、Map相互轉(zhuǎn)換的方法詳解,在實(shí)際項(xiàng)目開(kāi)發(fā)中或者一些算法面試題目中經(jīng)常需要用到Java中這三種類(lèi)型的相互轉(zhuǎn)換,比如對(duì)于一個(gè)整型數(shù)組中尋找一個(gè)整數(shù)與所給的一個(gè)整數(shù)值相同,需要的朋友可以參考下2023-08-08Java基礎(chǔ)教程之類(lèi)型轉(zhuǎn)換與多態(tài)
這篇文章主要介紹了Java基礎(chǔ)教程之類(lèi)型轉(zhuǎn)換與多態(tài),本文講解了 基本類(lèi)型轉(zhuǎn)換、 upcast與多態(tài)、 Object類(lèi)等內(nèi)容,需要的朋友可以參考下2014-09-09java9學(xué)習(xí)系列之安裝與jshell使用
2017年9月21日,千呼萬(wàn)喚始出來(lái),Java9終于發(fā)布了。作為自己天天接觸的“對(duì)象”,還是應(yīng)該多花點(diǎn)心思去了解她。后續(xù)再進(jìn)一步了解詳細(xì)特性。下面這篇文章主要給大家介紹了關(guān)于java9學(xué)習(xí)系列之安裝與jshell使用的相關(guān)資料,需要的朋友可以參考下。2017-09-09Spring Boot中使用JSR-303實(shí)現(xiàn)請(qǐng)求參數(shù)校驗(yàn)
這篇文章主要介紹了Spring Boot中使用JSR-303實(shí)現(xiàn)請(qǐng)求參數(shù)校驗(yàn),JSR-303校驗(yàn)我們一般都是對(duì)Java的實(shí)體類(lèi)對(duì)象進(jìn)行校驗(yàn),主要檢驗(yàn)JSR-303是Java中的一個(gè)規(guī)范,用于實(shí)現(xiàn)請(qǐng)求參數(shù)校驗(yàn)在我們的實(shí)體類(lèi)對(duì)象的屬性上,感興趣的朋友跟隨小編一起看看吧2023-10-10關(guān)于ThreadLocal對(duì)request和response的用法說(shuō)明
這篇文章主要介紹了關(guān)于ThreadLocal對(duì)request和response的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08Java在創(chuàng)建文件時(shí)指定編碼的實(shí)現(xiàn)方法
本文主要介紹了Java在創(chuàng)建文件時(shí)指定編碼的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringBoot如何集成PageHelper分頁(yè)功能
這篇文章主要介紹了SpringBoot如何集成PageHelper分頁(yè)功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03