spring-core-4 Spring EL表達(dá)式

4.1 介紹

Spring EL表達(dá)是一種強(qiáng)大的表達(dá)式語(yǔ)言, 可以支持在運(yùn)行時(shí)查詢和操作對(duì)象.它與EL類似, 但提供了更多擴(kuò)展功能,如方法調(diào)用,字符串模板功能.
Spring el表達(dá)式支持以下功能:

  • 字面值表達(dá)式
  • boolean和關(guān)系操作符
  • 正則表達(dá)式
  • 類表達(dá)式
  • 訪問(wèn)屬性,數(shù)組,集合
  • 方法調(diào)用
  • 關(guān)系運(yùn)算
  • 調(diào)用構(gòu)造函數(shù)
  • Bean引用
  • 構(gòu)建數(shù)組
  • 內(nèi)聯(lián)列表
  • 內(nèi)聯(lián)map
  • 三元操作
  • 變量
  • 用戶定義函數(shù)
  • 集合選擇
  • 模板表達(dá)式
4.2 計(jì)算

SpEL相關(guān)的類和接口位于org.springframework.expression包中.
ExpressionParser接口負(fù)責(zé)解析表達(dá)式字符串(用單引號(hào)包裹起來(lái)).
Expression接口負(fù)責(zé)計(jì)算表達(dá)式字符串.
此處可能拋出兩個(gè)異常:ParseException和EvaluationException`.
SpEL支持多種功能,如調(diào)用方法, 訪問(wèn)屬性,調(diào)用構(gòu)造函數(shù).
調(diào)用方法示例:

ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("'Hello World'.concat('!')");
String message = (String) exp.getValue();  // Hello World!

訪問(wèn)屬性示例:

ExpressionParser parser = new SpelExpressionParser();
// 此時(shí)會(huì)調(diào)用getBytes()方法
Expression exp = parser.parseExpression("'Hello World'.bytes");
byte[] bytes = (byte[]) exp.getValue();

SpEL也支持嵌套屬性.如p1.p2.p3
示例:

ExpressionParser parser = new SpelExpressionParser();
// 會(huì)調(diào)用 getBytes().length
Expression exp = parser.parseExpression("'Hello World'.bytes.length");
int length = (Integer) exp.getValue();

調(diào)用構(gòu)造函數(shù)示例:

ExpressionParser parser = new SpelExpressionParser();

Expression exp = parser.parseExpression("new String('hello world').toUpperCase()");
String msg = exp.getValue(String.class);

注意: 使用public <T> T getValue(Class<T> clazz);方法可以無(wú)需強(qiáng)轉(zhuǎn).但是如果結(jié)果轉(zhuǎn)換為T類型失敗,則會(huì)拋出EvaluationException.

SpEL最常見的用法是根據(jù)一個(gè)特定的對(duì)象實(shí)例(稱為根對(duì)象)求值.

// Create and set a calendar
GregorianCalendar c = new GregorianCalendar();
c.set(1856, 7, 9);

// The constructor arguments are name, birthday, and nationality.
Inventor tesla = new Inventor("Nikola Tesla", c.getTime(), "Serbian");

ExpressionParser parser = new SpelExpressionParser();

Expression exp = parser.parseExpression("name");
String name = (String) exp.getValue(tesla);
// name == "Nikola Tesla"

exp = parser.parseExpression("name == 'Nikola Tesla'");
boolean result = exp.getValue(tesla, Boolean.class);
// result == true
4.2.1 EvaluationContext

EvaluationContext接口在計(jì)算表達(dá)式時(shí)解析屬性,方法,字段及提供類型轉(zhuǎn)換.它有兩個(gè)開箱即用的實(shí)現(xiàn):

  • SimpleEvaluationContext: 提供了部分SpEL配置項(xiàng).它只支持SpEL的一個(gè)子集,它不支持JAVA類型引用,構(gòu)造函數(shù),及bean的引用. 要求顯式的配置對(duì)表達(dá)式中方法和屬性的支持級(jí)別.默認(rèn)情況下, 僅支持讀取屬性.但可以通過(guò)獲取一個(gè)builder來(lái)具體配置所需要的支持:如自定義PropertyAccessor(無(wú)反射), 數(shù)據(jù)綁定屬性的只讀訪問(wèn)或讀寫.
  • StandardEvaluationContext: 提供了全部的SpEL配置項(xiàng)

類型轉(zhuǎn)換
默認(rèn)情況下SpEL使用org.springframework.core.convert.ConversionService作為類型轉(zhuǎn)換服務(wù).
示例:

class Simple {
    public List<Boolean> booleanList = new ArrayList<Boolean>();
}

Simple simple = new Simple();
simple.booleanList.add(true);

EvaluationContext context = SimpleEvaluationContext().forReadOnlyDataBinding().build();

// 此處false是一個(gè)字符串. SpEL and the conversion service 將正確識(shí)別它并將其轉(zhuǎn)換為Boolean型.
parser.parseExpression("booleanList[0]").setValue(context, simple, "false");

// b will be false
Boolean b = simple.booleanList.get(0);
4.2.2 轉(zhuǎn)換配置

可以使用SpelParserConfiguration對(duì)象配置ExpressionParser. 配置對(duì)象能控制一些表達(dá)式組件的行為.如對(duì)于數(shù)組或集合, 指定其某個(gè)元素為null, 則可以自動(dòng)創(chuàng)建這個(gè)元素,如果索引超過(guò)了數(shù)組或列表的長(zhǎng)度, 則會(huì)自動(dòng)增加數(shù)組或列表以適應(yīng)這個(gè)索引.

class Demo {
    public List<String> list;
}

// Turn on:
// - auto null reference initialization
// - auto collection growing
SpelParserConfiguration config = new SpelParserConfiguration(true,true);

ExpressionParser parser = new SpelExpressionParser(config);

Expression expression = parser.parseExpression("list[3]");

Demo demo = new Demo();

Object o = expression.getValue(demo);

// demo.list will now be a real collection of 4 entries
// Each entry is a new empty String
4.2.3 SpEL編譯

spring 4.1引入了一個(gè)基本的表達(dá)式編譯器, 編譯器將在表達(dá)式計(jì)算期間動(dòng)態(tài)地生成一個(gè)真正的java類, 以便更快的解析表達(dá)式. 由于編譯器無(wú)法從表達(dá)式中知道引用的屬性的類型, 但是在第一次運(yùn)行時(shí)是可以知道的,當(dāng)然,如果你表達(dá)式引用的屬性類型會(huì)經(jīng)常變化, 這可能會(huì)帶來(lái)麻煩, 所以編譯只適合用于表達(dá)式引用的屬性類型不常變化的場(chǎng)景.
對(duì)于以下表達(dá)式:

someArray[0].someProperty.someOtherProperty < 0.1

這包含了數(shù)組訪問(wèn),屬性引用和數(shù)值操作.編譯后的性能提升非常明顯,在50000次迭代中, 不編譯需要75ms來(lái)完成, 編譯后只需要3ms.

編譯器配置
編譯器默認(rèn)是關(guān)閉的,但是它可以通過(guò)parser配置來(lái)開啟或一個(gè)系統(tǒng)屬性來(lái)開啟.
spring定義了幾種編譯器的操作模式(expression.spel.SpelCompolerMode):

  • OFF: 關(guān)閉編譯器
  • IMEDIATE: 立即模式, 表達(dá)式盡可能快的被編譯,這通常是在第一次計(jì)算表達(dá)式之后完成的,如果編譯失敗則會(huì)拋出異常.
  • MIXED: 混合模式, 在解釋模式(編譯器關(guān)閉時(shí)所用的方式)與編譯模式之間自動(dòng)切換, 如果編譯模式下發(fā)生了錯(cuò)誤,則會(huì)自動(dòng)切換到解釋模式.
    IMEDIATE模式的存在是由于MIXED模式可能會(huì)存在副作用, 比如在MIXED模式下,表達(dá)式在編譯模式下運(yùn)行了一半,就出現(xiàn)了異常,這時(shí)可能對(duì)某些系統(tǒng)狀態(tài)作了更改,此時(shí)它會(huì)自動(dòng)切換到解釋模式下,之前運(yùn)行過(guò)的部分會(huì)被再運(yùn)行一次,因此這時(shí)可能會(huì)引發(fā)某些問(wèn)題.
    示例, 使用SpelParserConfiguration配置編譯模式:
SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE, this.getClass().getClassLoader());
SpelExpressionParser parser = new SpelExpressionParser(config);
Expression exp = parser.parseExpression("payload");
MyMessge message = new MyMessage();
Object payload = exp.getValue(message);

在指定編譯器模式時(shí)也可以指定一個(gè)類加載器(可以為null), 編譯后的表達(dá)式將在提供了類加載器創(chuàng)建的子類加載器中定義.重要的是要確保提供的類加載器能夠看到表達(dá)式計(jì)算過(guò)程中所要調(diào)用的所有類.如果沒(méi)有提供, 則使用默認(rèn)加載器(通常在運(yùn)行時(shí)線程所在的上下文類加載器).

另一種配置方式是當(dāng)SpEL在其他組件中使用時(shí)可能無(wú)法通過(guò)上面的方式進(jìn)行配置,這時(shí)可以通過(guò)系統(tǒng)屬性spring.expression.compiler.mode來(lái)配置(值為前面提到的三種之一).

編譯器的限制
spring提供的編譯器不以編譯所有類型的表達(dá)式.以下幾種不被支持:

  • 賦值表達(dá)式
  • 依賴于轉(zhuǎn)換服務(wù)的表達(dá)式
  • 使用自定義解析器或訪問(wèn)器的表達(dá)式
  • expressions using selection or projection
4.3 bean定義中的表達(dá)式

SpEL可以用在XML或注解配置中.語(yǔ)法格式為#{ expression }.

4.3.1 XML配置

示例: 屬性或構(gòu)造函數(shù)中使用

<bean id="numberGuess" class="org.spring.samples.NumberGuess">
    <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
</bean>

使用systemProperties, 它是預(yù)定義的, 可以直接使用且不用在其前面添加#.如下:

<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
    <property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>
</bean>

通過(guò)bean名稱引用另一個(gè)bean的屬性:

<bean id="numberGuess" class="org.spring.samples.NumberGuess">
    <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>
</bean>

<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">
    <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>
</bean>
4.3.2 注解配置

在字段或方法上, 或者方法或構(gòu)造函數(shù)的參數(shù)中使用@Value注解.

public static class FieldValueTestBean
    // 在字段上使用
    @Value("#{ systemProperties['user.region'] }")
    private String defaultLocale;

    // setter getter
}

如下示例與上面示例等價(jià):

public static class PropertyValueTestBean

    private String defaultLocale;

    // set方法上使用
    @Value("#{ systemProperties['user.region'] }")
    public void setDefaultLocale(String defaultLocale) {
        this.defaultLocale = defaultLocale;
    }

    public String getDefaultLocale() {
        return this.defaultLocale;
    }

}

在@Autowired注解的構(gòu)造函數(shù)參數(shù)中使用:

public class SimpleMovieLister {

    private MovieFinder movieFinder;
    private String defaultLocale;

    @Autowired
    public void configure(MovieFinder movieFinder,
            @Value("#{ systemProperties['user.region'] }") String defaultLocale) {
        this.movieFinder = movieFinder;
        this.defaultLocale = defaultLocale;
    }

}
4.4 語(yǔ)法參考
4.4.1 字面值表達(dá)式

字面值表達(dá)式支持String, 數(shù)字(整數(shù),實(shí)數(shù),十六進(jìn)制), boolean, null.字面值要用單引號(hào)分隔, 要將單引號(hào)本身也引入其中,請(qǐng)使用兩個(gè)單引號(hào).通常不會(huì)單獨(dú)這樣使用,而是將其作為復(fù)雜表達(dá)式的一部分:

ExpressionParser parser = new SpelExpressionParser();

// 返回"Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
// 實(shí)數(shù)
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();

// 計(jì)算十六進(jìn)制值并返回2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
// 計(jì)算布爾值
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();

Object nullValue = parser.parseExpression("null").getValue();

數(shù)字支持使用負(fù)號(hào),指數(shù)符號(hào)和小數(shù)點(diǎn).

4.4.2 屬性, 數(shù)組, 集合, Map, 索引

訪問(wèn)屬性,用.符號(hào).允許對(duì)屬性名的第一個(gè)字母不分大小寫.

int year = (Integer) parser.parseExpression("Birthdate.Year + 1900").getValue(context);

String city = (String) parser.parseExpression("placeOfBirth.City").getValue(context);

數(shù)組或List

ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();

// Inventions Array

// evaluates to "Induction motor"
String invention = parser.parseExpression("inventions[3]").getValue(
        context, tesla, String.class);

// Members List

// evaluates to "Nikola Tesla"
String name = parser.parseExpression("Members[0].Name").getValue(
        context, ieee, String.class);

// List and Array navigation
// evaluates to "Wireless communication"
String invention = parser.parseExpression("Members[0].Inventions[6]").getValue(
        context, ieee, String.class);

訪問(wèn)Map, 由于Map的Key為String類型,因此可以直接指定key值.

// Officer's Dictionary

Inventor pupin = parser.parseExpression("Officers['president']").getValue(
        societyContext, Inventor.class);

// evaluates to "Idvor"
String city = parser.parseExpression("Officers['president'].PlaceOfBirth.City").getValue(
        societyContext, String.class);

// setting values
parser.parseExpression("Officers['advisors'][0].PlaceOfBirth.Country").setValue(
        societyContext, "Croatia");
內(nèi)聯(lián)List

可以內(nèi)聯(lián)使用{}直接表示List:

// evaluates to a Java list containing the four numbers
List numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context);

List listOfLists = (List) parser.parseExpression("{{'a','b'},{'x','y'}}").getValue(context);

注:{}本身就表示一個(gè)空的集合.

4.4.4 內(nèi)聯(lián)Map

Map也可以通過(guò){key:value}格式來(lái)直接定義.

// 將會(huì)得到包含兩個(gè)元素的java Map
Map inventorInfo = (Map) parser.parseExpression("{name:'Nikola',dob:'10-July-1856'}").getValue(context);

Map mapOfMaps = (Map) parser.parseExpression("{name:{first:'Nikola',last:'Tesla'},dob:{day:10,month:'July',year:1856}}").getValue(context);

注: {:}表示一個(gè)空的Map.Map的key上的引號(hào)是可選的.

4.4.5 構(gòu)建數(shù)組

可以用熟悉的java語(yǔ)法來(lái)構(gòu)建數(shù)組,同時(shí)也可提供初始化值.

int[] numbers1 = (int[]) parser.parseExpression("new int[4]").getValue(context);

// Array with initializer
int[] numbers2 = (int[]) parser.parseExpression("new int[]{1,2,3}").getValue(context);

// Multi dimensional array
int[][] numbers3 = (int[][]) parser.parseExpression("new int[4][5]").getValue(context);

注:日前不支持在構(gòu)建多維數(shù)據(jù)時(shí)提供初始化值.

4.4.6 方法調(diào)用

通過(guò)java語(yǔ)法格式, 可以直接對(duì)字符串調(diào)用方法,也支持可變參數(shù).

// string literal, evaluates to "bc"
String bc = parser.parseExpression("'abc'.substring(1, 3)").getValue(String.class);

// evaluates to true
boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(
        societyContext, Boolean.class);
4.4.7 操作符

關(guān)系操作符
關(guān)系操作符包括:==, !=, <, <=, >, >=.

// evaluates to true
boolean trueValue = parser.parseExpression("2 == 2").getValue(Boolean.class);

// evaluates to false
boolean falseValue = parser.parseExpression("2 < -5.0").getValue(Boolean.class);

// evaluates to true
boolean trueValue = parser.parseExpression("'black' < 'block'")
.getValue(Boolean.class);

大于或小于比較null值時(shí)遵循如下規(guī)則, null表示什么也沒(méi)有,因此任何值X與null進(jìn)行X>null都將為true, 反之也都將為false. 在使用數(shù)值比較時(shí),請(qǐng)不要與null進(jìn)行比較, 而是與0進(jìn)行比較.

除了標(biāo)準(zhǔn)的關(guān)系運(yùn)算符之外, SpEL還支持使用instanceof和正則表達(dá)式匹配計(jì)算:

/ evaluates to false
boolean falseValue = parser.parseExpression(
        "'xyz' instanceof T(Integer)").getValue(Boolean.class);

// evaluates to true
boolean trueValue = parser.parseExpression(
        "'5.00' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

//evaluates to false
boolean falseValue = parser.parseExpression(
        "'5.0067' matches '^-?\\d+(\\.\\d{2})?$'").getValue(Boolean.class);

在對(duì)基本類型使用instanceof時(shí)要注意, 因?yàn)樗鼤?huì)被裝箱,所以1 instanceof intfalse, 而1 instanceof Integertrue.
操作符可以用字母來(lái)代替, 這在xml配置中可以避免沖突.其字母代替為: lt(<), gt(>), le(<=), ge(>=), eq(==), ne(!=), div(/), mod(%), not(!). 這些不區(qū)分大小寫.

邏輯操作符
邏輯操作符為and, or, not.

// -- AND --

// evaluates to false
boolean falseValue = parser.parseExpression("true and false").getValue(Boolean.class);

// evaluates to true
String expression = "isMember('Nikola Tesla') and isMember('Mihajlo Pupin')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

// -- OR --

// evaluates to true
boolean trueValue = parser.parseExpression("true or false").getValue(Boolean.class);

// evaluates to true
String expression = "isMember('Nikola Tesla') or isMember('Albert Einstein')";
boolean trueValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

// -- NOT --用!表示

// evaluates to false
boolean falseValue = parser.parseExpression("!true").getValue(Boolean.class);

// -- AND and NOT --
String expression = "isMember('Nikola Tesla') and !isMember('Mihajlo Pupin')";
boolean falseValue = parser.parseExpression(expression).getValue(societyContext, Boolean.class);

數(shù)學(xué)操作
加號(hào)可以在操作數(shù)字和字符串, 減號(hào),乘號(hào)和除號(hào), 取余, 冪運(yùn)算只支持?jǐn)?shù)字.

// Addition 加號(hào)
int two = parser.parseExpression("1 + 1").getValue(Integer.class);  // 2

String testString = parser.parseExpression(
        "'test' + ' ' + 'string'").getValue(String.class);  // 'test string'

// Subtraction 減號(hào)
int four = parser.parseExpression("1 - -3").getValue(Integer.class);  // 4

double d = parser.parseExpression("1000.00 - 1e4").getValue(Double.class);  // -9000

// Multiplication 乘號(hào)
int six = parser.parseExpression("-2 * -3").getValue(Integer.class);  // 6

double twentyFour = parser.parseExpression("2.0 * 3e0 * 4").getValue(Double.class);  // 24.0

// Division 除號(hào)
int minusTwo = parser.parseExpression("6 / -3").getValue(Integer.class);  // -2

double one = parser.parseExpression("8.0 / 4e0 / 2").getValue(Double.class);  // 1.0

// Modulus 取余
int three = parser.parseExpression("7 % 4").getValue(Integer.class);  // 3

int one = parser.parseExpression("8 / 5 % 2").getValue(Integer.class);  // 1

// Operator precedence
int minusTwentyOne = parser.parseExpression("1+2-3*8").getValue(Integer.class);  // -21
4.4.8 賦值操作

設(shè)置屬性是通過(guò)賦值操作符來(lái)完成的,這通常是在setValue中完成的,但也可以在getValue中完成.

Inventor inventor = new Inventor();
EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();

parser.parseExpression("Name").setValue(context, inventor, "Aleksandar Seovic");

// alternatively
String aleks = parser.parseExpression(
        "Name = 'Aleksandar Seovic'").getValue(context, inventor, String.class);
4.4.9 類

特殊的T操作符可用來(lái)表示一個(gè)類的實(shí)例, 靜態(tài)方法也是用它來(lái)調(diào)用的.T操作符可以自動(dòng)發(fā)現(xiàn)java.lang包中的類,因此lang包中的類用簡(jiǎn)單類名即可,但是對(duì)于其它包中的類名, 必須使用全限定類名.

Class dateClass = parser.parseExpression("T(java.util.Date)").getValue(Class.class);

Class stringClass = parser.parseExpression("T(String)").getValue(Class.class);

boolean trueValue = parser.parseExpression(
        "T(java.math.RoundingMode).CEILING < T(java.math.RoundingMode).FLOOR")
        .getValue(Boolean.class);
4.4.10 構(gòu)造函數(shù)

可以通過(guò)new操作符調(diào)用構(gòu)造函數(shù).除了基本類型和String類之外, 都必須使用全限定類名.

Inventor einstein = p.parseExpression(
        "new org.spring.samples.spel.inventor.Inventor('Albert Einstein', 'German')")
        .getValue(Inventor.class);

//create new inventor instance within add method of List
p.parseExpression(
        "Members.add(new org.spring.samples.spel.inventor.Inventor(
            'Albert Einstein', 'German'))").getValue(societyContext);
4.4.11 變量

可以使用#varName格式引用變量.

Inventor tesla = new Inventor("Nikola Tesla", "Serbian");

EvaluationContext context = SimpleEvaluationContext.forReadWriteDataBinding().build();
context.setVariable("newName", "Mike Tesla");

parser.parseExpression("Name = #newName").getValue(context, tesla);
System.out.println(tesla.getName())  // "Mike Tesla"

#this和#root
#this總是表示當(dāng)前的evaluation對(duì)象.#root總是代表根上下文對(duì)象.雖然#this可能會(huì)隨著表達(dá)式組件的變化也變化, 但#root卻總是指向根對(duì)象的.

// 創(chuàng)建一個(gè)Integer類型的數(shù)組
List<Integer> primes = new ArrayList<Integer>();
primes.addAll(Arrays.asList(2,3,5,7,11,13,17));

// create parser and set variable 'primes' as the array of integers
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataAccess();
context.setVariable("primes", primes);

// all prime numbers > 10 from the list (using selection ?{...})
// evaluates to [11, 13, 17]
List<Integer> primesGreaterThanTen = (List<Integer>) parser.parseExpression(
        "#primes.?[#this>10]").getValue(context);
4.4.12 函數(shù)

你可以通過(guò)定義能在表達(dá)式字符串中被調(diào)用的自定義函數(shù)來(lái)擴(kuò)展SpEL.

Method method = ...;

EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
context.setVariable("myFunction", method);
public abstract class StringUtils {

    public static String reverseString(String input) {
        StringBuilder backwards = new StringBuilder(input.length());
        for (int i = 0; i < input.length(); i++)
            backwards.append(input.charAt(input.length() - 1 - i));
        }
        return backwards.toString();
    }
}
ExpressionParser parser = new SpelExpressionParser();

EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
context.setVariable("reverseString",
        StringUtils.class.getDeclaredMethod("reverseString", String.class));

String helloWorldReversed = parser.parseExpression(
        "#reverseString('hello')").getValue(context, String.class);
4.4.13 Bean引用

如果配置了evaluation context中配置了bean解析器, 則可以用@符號(hào)查找bean.

ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());

// This will end up calling resolve(context,"foo") on MyBeanResolver during evaluation
Object bean = parser.parseExpression("@foo").getValue(context);

如果要訪問(wèn)factory bean本身, 應(yīng)使用&標(biāo)識(shí)符.

ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setBeanResolver(new MyBeanResolver());

// This will end up calling resolve(context,"&foo") on MyBeanResolver during evaluation
Object bean = parser.parseExpression("&foo").getValue(context);
4.4.14 三元操作符
String falseString = parser.parseExpression(
        "false ? 'trueExp' : 'falseExp'").getValue(String.class);
parser.parseExpression("Name").setValue(societyContext, "IEEE");
societyContext.setVariable("queryName", "Nikola Tesla");

expression = "isMember(#queryName)? #queryName + ' is a member of the ' " +
        "+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'";

String queryResultString = parser.parseExpression(expression)
        .getValue(societyContext, String.class);
// queryResultString = "Nikola Tesla is a member of the IEEE Society"
```

######4.4.15 Elvis 操作符
這是三元操作符的簡(jiǎn)寫形式
```
ExpressionParser parser = new SpelExpressionParser();

String name = parser.parseExpression("name?:'Unknown'").getValue(String.class);
System.out.println(name);  // 'Unknown'
```
> 可用它來(lái)為屬性設(shè)置默認(rèn)值`@Value("#{systemProperties['pop3.port'] ?: 25}")`

######4.4.16 安全導(dǎo)航操作
當(dāng)引用一個(gè)對(duì)象的屬性時(shí)通過(guò)要對(duì)其作null判斷, 如果為null, 則會(huì)拋出NPE, 安全導(dǎo)航操作則是為了避免NPE, 并返回簡(jiǎn)單的null.
ExpressionParser parser = new SpelExpressionParser();
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();

Inventor tesla = new Inventor("Nikola Tesla", "Serbian");
tesla.setPlaceOfBirth(new PlaceOfBirth("Smiljan"));

String city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, tesla, String.class);
System.out.println(city);  // Smiljan

tesla.setPlaceOfBirth(null);
city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, tesla, String.class);
System.out.println(city);  // null - does not throw NullPointerException!!!

######4.4.17 集合選擇
集合選擇是一個(gè)強(qiáng)大的表達(dá)式語(yǔ)言特性, 它允許你選擇源集合中的一些元素來(lái)組成一個(gè)新的集合.
其語(yǔ)法格式為:`.?[expression]`.這會(huì)過(guò)濾集合并返回包含原集合的一個(gè)子集的新集合.
```
List<Inventor> list = (List<Inventor>) parser.parseExpression(
        "Members.?[Nationality == 'Serbian']").getValue(societyContext);
```
在列表和Map上都可以使用,list是對(duì)每個(gè)元素進(jìn)行選擇, map則是對(duì)每個(gè)entry進(jìn)行選擇.因此Map可以將key或value作為表達(dá)式屬性進(jìn)行選擇.
如下,選擇集合的value小于27的值.
```
Map newMap = parser.parseExpression("map.?[value<27]").getValue();
```
**注:**除了返回被選擇的元素, 還可以檢索第一個(gè)或最后一個(gè)元素.其語(yǔ)法分別是`.^[expression]`和`.$[experssion]`.

######4.4.18 集合Projection
Projection允許一個(gè)集合驅(qū)動(dòng)一個(gè)子表達(dá)式并返回一個(gè)新的集合, 其語(yǔ)法是: `.![expression]`. 
假設(shè)我們有一個(gè)inventors list, 現(xiàn)在我們想要找出他們出生的城市的集合.
```
// returns ['Smiljan', 'Idvor' ]
List placesOfBirth = (List)parser.parseExpression("Members.![placeOfBirth.city]");
```
針對(duì)Map也可以使用.獲取的結(jié)果是一個(gè)list.

######4.4.19 表達(dá)式模板
表達(dá)式模板是指可以通過(guò)表達(dá)式前綴和后綴來(lái)組合表達(dá)式.
```
String randomPhrase = parser.parseExpression(
        "random number is #{T(java.lang.Math).random()}",
        new TemplateParserContext()).getValue(String.class);
```
這個(gè)表達(dá)式的執(zhí)行結(jié)果是將`random number is`和`#{}`中的執(zhí)行結(jié)果拼接起來(lái), 第二個(gè)參數(shù)是一個(gè)`ParserContext`類型, 用來(lái)定義表達(dá)式的解析方法, 其定義如下:
```
public class TemplateParserContext implements ParserContext {

    public String getExpressionPrefix() {
        return "#{";
    }

    public String getExpressionSuffix() {
        return "}";
    }

    public boolean isTemplate() {
        return true;
    }
}
```

#####4.5 前面示例中使用的類
Inventor.java
```
package org.spring.samples.spel.inventor;

import java.util.Date;
import java.util.GregorianCalendar;

public class Inventor {

    private String name;
    private String nationality;
    private String[] inventions;
    private Date birthdate;
    private PlaceOfBirth placeOfBirth;

    public Inventor(String name, String nationality) {
        GregorianCalendar c= new GregorianCalendar();
        this.name = name;
        this.nationality = nationality;
        this.birthdate = c.getTime();
    }

    public Inventor(String name, Date birthdate, String nationality) {
        this.name = name;
        this.nationality = nationality;
        this.birthdate = birthdate;
    }

    public Inventor() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNationality() {
        return nationality;
    }

    public void setNationality(String nationality) {
        this.nationality = nationality;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }

    public PlaceOfBirth getPlaceOfBirth() {
        return placeOfBirth;
    }

    public void setPlaceOfBirth(PlaceOfBirth placeOfBirth) {
        this.placeOfBirth = placeOfBirth;
    }

    public void setInventions(String[] inventions) {
        this.inventions = inventions;
    }

    public String[] getInventions() {
        return inventions;
    }
}
```

PlaceOfBirth.java
```
package org.spring.samples.spel.inventor;

public class PlaceOfBirth {

    private String city;
    private String country;

    public PlaceOfBirth(String city) {
        this.city=city;
    }

    public PlaceOfBirth(String city, String country) {
        this(city);
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String s) {
        this.city = s;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

}
```

Society.java
```
package org.spring.samples.spel.inventor;

import java.util.*;

public class Society {

    private String name;

    public static String Advisors = "advisors";
    public static String President = "president";

    private List<Inventor> members = new ArrayList<Inventor>();
    private Map officers = new HashMap();

    public List getMembers() {
        return members;
    }

    public Map getOfficers() {
        return officers;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isMember(String name) {
        for (Inventor inventor : members) {
            if (inventor.getName().equals(name)) {
                return true;
            }
        }
        return false;
    }

}
```
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容