溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Spring中怎么利用Expression接口實(shí)現(xiàn)表達(dá)式求值操作

發(fā)布時(shí)間:2021-07-26 14:09:31 來(lái)源:億速云 閱讀:122 作者:Leah 欄目:編程語(yǔ)言

這篇文章將為大家詳細(xì)講解有關(guān)Spring中怎么利用Expression接口實(shí)現(xiàn)表達(dá)式求值操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

一 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é)果:10new String('helloworld').toUpperCase()的結(jié)果是: HELLOWORLD以persn為root,name表達(dá)式的值是: 孫悟空truelist集合的第一個(gè)元素為:false

關(guān)于Spring中怎么利用Expression接口實(shí)現(xiàn)表達(dá)式求值操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI