麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

【Spring】Spring Framework Reference Documentation中文版9

2019-11-11 05:10:13
字體:
來源:轉載
供稿:網友

10. SPRing Expression Language (SpEL)

spring的表達式語言

 

10.1 Introduction

介紹

 

The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.

spring表達式語言(使用SpEL來簡化)是一個強大的表達式語言用于支持查詢和管理object在運行時。語言的語法和統一的EL類似但是提供了其他的特性,大部分方法的調用和基本的字符串模板功能。

 

While there are several other java expression languages available, OGNL, MVEL, and JBoss EL, to name a few, the Spring Expression Language was created to provide the Spring community with a single well supported expression language that can be used across all the products in the Spring portfolio. Its language features are driven by the requirements of the projects in the Spring portfolio, including tooling requirements for code completion support within the eclipse based Spring Tool Suite. That said, SpEL is based on a technology agnostic API allowing other expression language implementations to be integrated should the need arise.

目前有一個其他的java表達式語言,如OGNL、MVEL和JBoss EL,為了區別,spring表達式語言是spring社區使用的一個簡單的支持表達式語言可以用于跨spring的產品。語言特性通過需要定義在spring的程序中,包括代碼完成支持的工具如基于eclipse的spring工具箱。SpEL是一個基于技術不可知的API允許其他的表達式語言實現用于集成需要的。

 

While SpEL serves as the foundation for expression evaluation within the Spring portfolio, it is not directly tied to Spring and can be used independently. In order to be self contained, many of the examples in this chapter use SpEL as if it were an independent expression language. This requires creating a few bootstrapping infrastructure classes such as the parser. Most Spring users will not need to deal with this infrastructure and will instead only author expression strings for evaluation. An example of this typical use is the integration of SpEL into creating xml or annotated based bean definitions as shown in the section Expression support for defining bean definitions.

SpEL服務作為一個基礎用于表達式在spring中,并沒有直接和spring相關聯可以獨立使用。為了自包含,在這節中許多的例子使用SpEL由于它是一個獨立的表達式語言。允許創建一些基礎設置類例如解析器。大部分spring的用戶將不需要處理基礎架構只是用于評估的表達式字符串。一個典型的案例使用就是在創建XML中集成SpEL或注解bean的定義就像在“表達式支持用于bean的定義”中描述的一樣。

 

This chapter covers the features of the expression language, its API, and its language syntax. In several places an Inventor and Inventor’s Society class are used as the target objects for expression evaluation. These class declarations and the data used to populate them are listed at the end of the chapter.

這一節包含了表達式語言的特性、API和語法。在一些地方Inventor和Inventor的Society類用在目標object用于表達式評估。這些類定義和數據通常組成他們并列在章節的末尾。

 

10.2 Feature Overview

特性概覽

 

The expression language supports the following functionality

表達式語言支持如下的功能

 

    Literal expressions

文字化表達

    Boolean and relational Operators

布爾和關系操作符

    Regular expressions

正則表達式

    Class expressions

類表達式

    accessing properties, arrays, lists, maps

訪問屬性、數組、list和map

    Method invocation

方法調用

    Relational operators

關系操作符

    Assignment

聲明

    Calling constructors

調用構造器

    Bean references

bean的引用

    Array construction

數組的構造

    Inline lists

內嵌的list

    Inline maps

內嵌的map

    Ternary operator

三目表達式

    Variables

變量

    User defined functions

用戶自定義函數

    Collection projection

集合映射

    Collection selection

集合選擇

    Templated expressions

模板表達式

 

10.3 Expression Evaluation using Spring’s Expression Interface

使用spring的表達式接口來評估表達式

 

This section introduces the simple use of SpEL interfaces and its expression language. The complete language reference can be found in the section Language Reference.

這節介紹簡要使用SpEL接口和他的表達式語言。詳細的語言參考可以在“語言參考”這一節中找到。

 

The following code introduces the SpEL API to evaluate the literal string expression 'Hello World'.

下面的代碼使用了SpEL的API用于評價字符串表達式“Hello World”

 

ExpressionParser parser = new SpelExpressionParser();

Expression exp = parser.parseExpression("'Hello World'");

String message = (String) exp.getValue();

 

The value of the message variable is simply 'Hello World'.

消息變量的值是"Hello World"

 

The SpEL classes and interfaces you are most likely to use are located in the packages org.springframework.expression and its sub packages and spel.support.

你最常用的SpEL類和接口在org.springframework.expression包中,包括他的子包spel.support。

 

The interface ExpressionParser is responsible for parsing an expression string. In this example the expression string is a string literal denoted by the surrounding single quotes. The interface Expression is responsible for evaluating the previously defined expression string. There are two exceptions that can be thrown, ParseException and EvaluationException when calling parser.parseExpression and exp.getValue respectively.

ExpressionParser接口用于解析表達式字符串。在這個例子中表達式字符串是一個通過單引號包圍的文本形式字符串。Expression接口用于解析之前定義的表達式字符串。有兩個異??赡軙粧伋?,ParseException和EvaluationException當分別調用parser.parseExpression和exp.getValue。

 

SpEL supports a wide range of features, such as calling methods, accessing properties, and calling constructors.

SpEL支持許多特性,例如調用方法、訪問屬性和訪問構造器。

 

As an example of method invocation, we call the concat method on the string literal.

作為方法調用的例子,我們可以在字符串上調用concat方法。

 

ExpressionParser parser = new SpelExpressionParser();

Expression exp = parser.parseExpression("'Hello World'.concat('!')");

String message = (String) exp.getValue();

 

The value of message is now 'Hello World!'.

消息的值現在是"Hello World"

 

As an example of calling a JavaBean property, the String property Bytes can be called as shown below.

作為一個調用JavaBean屬性的例子,字符串屬性可以如下的方式被使用。

 

ExpressionParser parser = new SpelExpressionParser();

 

// invokes 'getBytes()'

// 調用getBytes()

Expression exp = parser.parseExpression("'Hello World'.bytes");

byte[] bytes = (byte[]) exp.getValue();

 

SpEL also supports nested properties using standard dot notation, i.e. prop1.prop2.prop3 and the setting of property values

SpEL也支持內置的屬性通過使用點符號,例如,prop1.prop2.prop3和屬性的設置

 

Public fields may also be accessed.

公用的域也可以被訪問

 

ExpressionParser parser = new SpelExpressionParser();

 

// invokes 'getBytes().length'

// 調用getBytes().length

Expression exp = parser.parseExpression("'Hello World'.bytes.length");

int length = (Integer) exp.getValue();

 

The String’s constructor can be called instead of using a string literal.

字符串的構造器可以通過使用文本方式來進行替代

 

ExpressionParser parser = new SpelExpressionParser();

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

String message = exp.getValue(String.class);

 

Note the use of the generic method public <T> T getValue(Class<T> desiredResultType). Using this method removes the need to cast the value of the expression to the desired result type. An EvaluationException will be thrown if the value cannot be cast to the type T or converted using the registered type converter.

注意通用的方法public <T> T getValue(Class<T> desiredResultType)的使用。使用這樣的方法去掉了將表達式的值轉換為目標的結果類型。如果值不能轉化為T類型或轉換使用了注冊的類型轉換器將拋出EvaluationException異常。

 

The more common usage of SpEL is to provide an expression string that is evaluated against a specific object instance (called the root object). There are two options here and which to choose depends on whether the object against which the expression is being evaluated will be changing with each call to evaluate the expression. In the following example we retrieve the name property from an instance of the Inventor class.

通常使用SpEL是用于提供一個表達式字符串處理特定的object實例(叫做root object)。這里有兩種方法可以選擇依賴于被處理的object在使用時是否會改變在調用表達式的時候。在下面的例子中我們獲得了Inventor類中的name屬性。

 

// Create and set a calendar

// 創建并設置一個calendar

GregorianCalendar c = new GregorianCalendar();

c.set(1856, 7, 9);

 

// The constructor arguments are name, birthday, and nationality.

// 構造器屬性是name、birthday、nationality

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

 

ExpressionParser parser = new SpelExpressionParser();

Expression exp = parser.parseExpression("name");

 

EvaluationContext context = new StandardEvaluationContext(tesla);

String name = (String) exp.getValue(context);

 

In the last line, the value of the string variable name will be set to "Nikola Tesla". The class StandardEvaluationContext is where you can specify which object the "name" property will be evaluated against. This is the mechanism to use if the root object is unlikely to change, it can simply be set once in the evaluation context. If the root object is likely to change repeatedly, it can be supplied on each call to getValue, as this next example shows:

在最后一行,字符串變量name的值是"Nikola Tesla"。使用StandardEvaluationContext你可以處理name屬性的值。如果root object沒有被改變過時的一種策略,可以在上下文中直接設置。如果root object的值反復被改變,可以類似于下面例子的說明調用getValue:

 

// Create and set a calendar

// 創建并設置一個calendar

GregorianCalendar c = new GregorianCalendar();

c.set(1856, 7, 9);

 

// The constructor arguments are name, birthday, and nationality.

// 構造器參數是name、birthday和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);

 

In this case the inventor tesla has been supplied directly to getValue and the expression evaluation infrastructure creates and manages a default evaluation context internally - it did not require one to be supplied.

在這個例子中使用inventor tesla來直接調用getValue和表達式處理創建和管理默認的上下文則不需要被提供。

 

The StandardEvaluationContext is relatively expensive to construct and during repeated usage it builds up cached state that enables subsequent expression evaluations to be performed more quickly. For this reason it is better to cache and reuse them where possible, rather than construct a new one for each expression evaluation.

StandardEvaluationContext的創建是比較消耗資源,并且在重復使用中會產生緩存狀態允許后面的表達式可以迅速得到處理。在這種情況下最好根據需要緩存和重復使用他們而不是在每個表達式處理中創建一個新實例。

 

In some cases it can be desirable to use a configured evaluation context and yet still supply a different root object on each call to getValue. getValue allows both to be specified on the same call. In these situations the root object passed on the call is considered to override any (which maybe null) specified on the evaluation context.

在一些情況下可以使用配置上下文和提供不同的root object在每次調用getValue時。getValue允許在同一次調用中進行定制化。在這些情況下,被調用的root object覆蓋任何特定一項(或許是null)在上下文中。

 

[Note]

注意

 

In standalone usage of SpEL there is a need to create the parser, parse expressions and perhaps provide evaluation contexts and a root context object. However, more common usage is to provide only the SpEL expression string as part of a configuration file, for example for Spring bean or Spring Web Flow definitions. In this case, the parser, evaluation context, root object and any predefined variables are all set up implicitly, requiring the user to specify nothing other than the expressions.

單獨使用SpEL需要創建解析器,解析表達式和提供上下文和root上下文object。然而,大部分情況下只有SpEL表達式作為配置文件的一部分,例如對于spring的bean或spring的web流定義。在這些情況下,解析器、上下文、root object和其他提前定義的變量都被隱含的設置,需要用戶定義表達式而不需要定義其他內容。

 

As a final introductory example, the use of a boolean operator is shown using the Inventor object in the previous example.

作為最后一個例子,使用了boolean表達式用于展示在前面的例子中使用Inventor的object。

 

Expression exp = parser.parseExpression("name == 'Nikola Tesla'");

boolean result = exp.getValue(context, Boolean.class); // evaluates to true

 

10.3.1 The EvaluationContext interface

EvaluationContext接口

 

The interface EvaluationContext is used when evaluating an expression to resolve properties, methods, fields, and to help perform type conversion. The out-of-the-box implementation, StandardEvaluationContext, uses reflection to manipulate the object, caching java.lang.reflect.Method, java.lang.reflect.Field, and java.lang.reflect.Constructor instances for increased performance.

EvaluationContext接口處理表達式用于處理屬性、方法、域和幫助提供類型轉換。外部的實現有StandardEvaluationContext使用反射來處理object,調用java.lang.reflect.Method、java.lang.reflect.Field和java.lang.reflect.Constructor實例用于提高性能。

 

The StandardEvaluationContext is where you may specify the root object to evaluate against via the method setRootObject() or passing the root object into the constructor. You can also specify variables and functions that will be used in the expression using the methods setVariable() and registerFunction(). The use of variables and functions are described in the language reference sections Variables and Functions. The StandardEvaluationContext is also where you can register custom ConstructorResolvers, MethodResolvers, and PropertyAccessors to extend how SpEL evaluates expressions. Please refer to the JavaDoc of these classes for more details.

StandardEvaluationContext你可以用來指定root object來處理通過setRootObject方法或將root object傳遞給構造器。你也可以指定變量和函數使用在表達式中通過方法setVariable和registerFunction。變量和函數的使用在語言參考文檔章節名是變量和函數。StandardEvaluationContext也可以用來注冊自定義的ConstructorResolvers、MethodResolvers和PropertyAccessors用來擴展SpEL處理表達式。請參考Javadocs獲取更多信息。

 

Type Conversion

類型轉換

 

By default SpEL uses the conversion service available in Spring core ( org.springframework.core.convert.ConversionService). This conversion service comes with many converters built in for common conversions but is also fully extensible so custom conversions between types can be added. Additionally it has the key capability that it is generics aware. This means that when working with generic types in expressions, SpEL will attempt conversions to maintain type correctness for any objects it encounters.

通過默認的SpEL使用轉換服務在spring core中是可行的(org.springframework.core.convert.ConversionService)。轉換服務由內置的多個轉換器來支持用于通用的轉換但是也支持擴展使得自定義轉換器可以被添加。額外的,也支持關鍵字識別。這意味著可以在表達式中使用泛型,SpEL將會嘗試轉換類型的準確性對于處理的任何object。

 

What does this mean in practice? Suppose assignment, using setValue(), is being used to set a List property. The type of the property is actually List<Boolean>. SpEL will recognize that the elements of the list need to be converted to Boolean before being placed in it. A simple example:

在練習這意味著什么?假設分配屬性,使用setValue,可以用于設置一個list屬性。屬性的類型實際上是List<Boolean>。SpEL將會識別到list中元素的類型需要轉換為Boolean在其被放置到list中之前。一個簡單的例子:

 

class Simple {

    public List<Boolean> booleanList = new ArrayList<Boolean>();

}

 

Simple simple = new Simple();

 

simple.booleanList.add(true);

 

StandardEvaluationContext simpleContext = new StandardEvaluationContext(simple);

 

// false is passed in here as a string. SpEL and the conversion service will

// correctly recognize that it needs to be a Boolean and convert it

// false在這里通過字符串的形式傳遞。SpEL和轉換服務將正確識別到需要轉換為boolean然后進行轉換

parser.parseExpression("booleanList[0]").setValue(simpleContext, "false");

 

// b will be false

// b的值是false

Boolean b = simple.booleanList.get(0);

 

10.3.2 Parser configuration

解析器配置

 

It is possible to configure the SpEL expression parser using a parser configuration object (org.springframework.expression.spel.SpelParserConfiguration). The configuration object controls the behavior of some of the expression components. For example, if indexing into an array or collection and the element at the specified index is null it is possible to automatically create the element. This is useful when using expressions made up of a chain of property references. If indexing into an array or list and specifying an index that is beyond the end of the current size of the array or list it is possible to automatically grow the array or list to accommodate that index.

配置SpEL表達式解析器通過使用一個解析器配置object是可以的(org.springframework.expression.spel.SpelParserConfiguration)。配置object控制一些表達式組件的行為。例如,如果在數組或集合中索引,元素在特定的索引是null可以自動創建元素。當使用表達式組成屬性引用鏈時這種方式是有用的。如果在數組或list中索引,并且指定索引取決于當前數組或list的大小的結尾可以自動增加直到索引的末尾。

 

class Demo {

    public List<String> list;

}

 

// Turn on:

// - auto null reference initialization

// 自動null引用初始化

// - 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

// demo.list現在是一個用于4個元素的集合

// Each entry is a new empty String

// 每個元素是一個空的字符串

 

It is also possible to configure the behaviour of the SpEL expression compiler.

也可以配置SpEL表達式編譯器的行為

 

10.3.3 SpEL compilation

SpEL編譯

 

Spring Framework 4.1 includes a basic expression compiler. Expressions are usually interpreted which provides a lot of dynamic flexibility during evaluation but does not provide the optimum performance. For occasional expression usage this is fine, but when used by other components like Spring Integration, performance can be very important and there is no real need for the dynamism.

spring框架4.1包括基本的表達式編譯器。表達式通常被理解通過提供許多動柔度在處理過程中但是不提供性能的操作性。對于偶然的表達式使用也是可以的,但是當用于其他的組件例如spring集成時,性能將會很重要并且不需要動態的特性的。

 

The new SpEL compiler is intended to address this need. The compiler will generate a real Java class on the fly during evaluation that embodies the expression behavior and use that to achieve much faster expression evaluation. Due to the lack of typing around expressions the compiler uses information gathered during the interpreted evaluations of an expression when performing compilation. For example, it does not know the type of a property reference purely from the expression but during the first interpreted evaluation it will find out what it is. Of course, basing the compilation on this information could cause trouble later if the types of the various expression elements change over time. For this reason compilation is best suited to expressions whose type information is not going to change on repeated evaluations.

新的SpEL編譯器試圖滿足這樣的需要。編譯器將會生成一個實際的Java類在處理期間并體現了表達式行為并且可以獲得更快的表達式處理速度。由于缺少輸入的表達式編譯器使用信息收集當處理編譯表達式的過程中。例如,不需要知道屬性的類型引用通過表達式但是在第一次處理時找到。當然,基于編譯這些信息可能會在后續引起問題如果不同表達式元素發生了改變。由于這種原因編譯是最適合表達類型的信息而不需要在重復的評估中改變。

 

For a basic expression like this:

對于一個基本的表達式如下:

 

someArray[0].someProperty.someOtherProperty < 0.1

 

which involves array access, some property derefencing and numeric operations, the performance gain can be very noticeable. In an example micro benchmark run of 50000 iterations, it was taking 75ms to evaluate using only the interpreter and just 3ms using the compiled version of the expression.

調用數組訪問、屬性的間接引用和數字的操作,性能的提升是顯而易見的。在一個例子中微機分數運行5萬次迭代,只花費了75毫秒來攔截并且只有3毫秒用于編譯表達式的版本。

 

Compiler configuration

編譯器配置

 

The compiler is not turned on by default, but there are two ways to turn it on. It can be turned on using the parser configuration process discussed earlier or via a system property when SpEL usage is embedded inside another component. This section discusses both of these options.

編譯器在默認情況下并沒有開啟,但是有兩種方式來打開它??梢允褂媒馕雠渲眠M程在前面討論過的或通過系統屬性當SpEL使用在另一個內置的組件中。這一節將討論這三個選項。

 

Is is important to understand that there are a few modes the compiler can operate in, captured in an enum (org.springframework.expression.spel.SpelCompilerMode). The modes are as follows:

理解有一些模式可以讓編譯器操作是重要的,設置在枚舉中(org.springframework.expression.spel.SpelCompilerMode)。這些模式包括:

 

    OFF - The compiler is switched off; this is the default.

OFF - 編譯器是關閉的,這是默認值

    IMMEDIATE - In immediate mode the expressions are compiled as soon as possible. This is typically after the first interpreted evaluation. If the compiled expression fails (typically due to a type changing, as described above) then the caller of the expression evaluation will receive an exception.

IMMEDIATE - 在這個模式下表達式會以最快的速度編譯。通常在第一次運行之后。如果編譯表達式失敗(通常由于類型改變,在上面描述過)然后調用表達式會收到一個異常。

    MIXED - In mixed mode the expressions silently switch between interpreted and compiled mode over time. After some number of interpreted runs they will switch to compiled form and if something goes wrong with the compiled form (like a type changing, as described above) then the expression will automatically switch back to interpreted form again. Sometime later it may generate another compiled form and switch to it. Basically the exception that the user gets in IMMEDIATE mode is instead handled internally.

MIXED - 在這個模式下表達式會安靜的切換在理解和編譯模式間。在一些攔截器運行后他們將會轉換編譯形式并且如果一些問題在編譯中產生(例如類型改變,在上面描述過)表達式將會再一次自動切換回攔截。有時可能會產生另一種編譯形式然后轉換至此?;居脩臬@得的異常在IMMEDIATE模式會在內部被處理。

 

IMMEDIATE mode exists because MIXED mode could cause issues for expressions that have side effects. If a compiled expression blows up after partially succeeding it may have already done something that has affected the state of the system. If this has happened the caller may not want it to silently re-run in interpreted mode since part of the expression may be running twice.

IMMEDIATE模式存在是因為MIXED模式在處理表達式時可能產生問題并有其他的影響。如果一個編譯表達式在部分成功的情況下,可能已經影響了系統的某些狀態。如果已經出現這種情況,調用者可能不希望重新執行程序因為有些有些表達式可能會運行兩次。

 

After selecting a mode, use the SpelParserConfiguration to configure the parser:

當選擇一種模式后,使用SpelParserConfiguration來配置解析器:

 

SpelParserConfiguration config = new SpelParserConfiguration(SpelCompilerMode.IMMEDIATE,

    this.getClass().getClassLoader());

 

SpelExpressionParser parser = new SpelExpressionParser(config);

 

Expression expr = parser.parseExpression("payload");

 

MyMessage message = new MyMessage();

 

Object payload = expr.getValue(message);

 

When specifying the compiler mode it is also possible to specify a classloader (passing null is allowed). Compiled expressions will be defined in a child classloader created under any that is supplied. It is important to ensure if a classloader is specified it can see all the types involved in the expression evaluation process. If none is specified then a default classloader will be used (typically the context classloader for the thread that is running during expression evaluation).

當指定編譯器模式時也可以指定一個類加載器(傳遞null也是允許的)。編譯表達式將會被定義在被提供的子類加載器中創建。如果定義了一個類加載器確保他可以看到所有表達式處理的所有類型是很重要的。如果沒有定義則默認的類加載器將會被使用(通常用于線程的上下文加載器在表達式處理時會執行)

 

The second way to configure the compiler is for use when SpEL is embedded inside some other component and it may not be possible to configure via a configuration object. In these cases it is possible to use a system property. The property spring.expression.compiler.mode can be set to one of the SpelCompilerMode enum values (off, immediate, or mixed).

第二個方法配置編譯器是為了用于當SpEL被嵌入到其他的組件并且可能不能通過configuration來配置。在這種情況下可以使用系統屬性。spring.expression.compiler.mode屬性可以被設置為SpelCompilerMode的其中一個枚舉值(off、immediate或mixed)

 

Compiler limitations

編譯器限制

 

With Spring Framework 4.1 the basic compilation framework is in place. However, the framework does not yet support compiling every kind of expression. The initial focus has been on the common expressions that are likely to be used in performance critical contexts. These kinds of expression cannot be compiled at the moment:

在spring框架4.1中有基本的編譯框架。然而,框架不支持編譯每一種表達式。重點會關注在通用的表達式可以在一些嚴酷性能的上下文中運行。這是這些表達式不能被編譯:

 

    expressions involving assignment

用于分配的表達式

    expressions relying on the conversion service

依賴轉換服務的表達式

    expressions using custom resolvers or accessors

使用自定義解析器或訪問器的表達式

    expressions using selection or projection

使用選擇或映射的表達式

 

More and more types of expression will be compilable in the future.

在未來越來越多類型的表達式將會被編譯。

 

10.4 Expression support for defining bean definitions

用于定義bean定義的表達式支持

 

SpEL expressions can be used with XML or annotation-based configuration metadata for defining BeanDefinitions. In both cases the syntax to define the expression is of the form #{ <expression string> }.

SpEL表達式可以通過XML或基于注解的配置用于定義bean定義。在相同的情況下定義表達式的語法類似于#{<表達式字符串>}

 

10.4.1 XML based configuration

基于xml的配置

 

A property or constructor-arg value can be set using expressions as shown below.

屬性或構造器參數值可以被設置用于表達式,例子如下。

 

<bean id="numberGuess" class="org.spring.samples.NumberGuess">

    <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>

 

    <!-- other properties -->

</bean>

 

The variable systemProperties is predefined, so you can use it in your expressions as shown below. Note that you do not have to prefix the predefined variable with the # symbol in this context.

systemProperties之前已經定義,因此你可以如下使用你的表達式。注意你不能在之前定義的變量前增加前綴#在上下文中。

 

<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">

    <property name="defaultLocale" value="#{ systemProperties['user.region'] }"/>

 

    <!-- other properties -->

</bean>

 

You can also refer to other bean properties by name, for example.

你也可以通過name引用其他的bean屬性,例如。

 

<bean id="numberGuess" class="org.spring.samples.NumberGuess">

    <property name="randomNumber" value="#{ T(java.lang.Math).random() * 100.0 }"/>

 

    <!-- other properties -->

</bean>

 

<bean id="shapeGuess" class="org.spring.samples.ShapeGuess">

    <property name="initialShapeSeed" value="#{ numberGuess.randomNumber }"/>

 

    <!-- other properties -->

</bean>

 

10.4.2 Annotation-based configuration

基于注解的配置

 

The @Value annotation can be placed on fields, methods and method/constructor parameters to specify a default value.

@Value注解可以修飾屬性、方法、方法/構造方法參數用于定義默認值。

 

Here is an example to set the default value of a field variable.

下面是一個為屬性設置默認值的例子。

 

public static class FieldValueTestBean

 

    @Value("#{ systemProperties['user.region'] }")

    private String defaultLocale;

 

    public void setDefaultLocale(String defaultLocale) {

        this.defaultLocale = defaultLocale;

    }

 

    public String getDefaultLocale() {

        return this.defaultLocale;

    }

 

}

 

The equivalent but on a property setter method is shown below.

相同但是下面的例子是修飾在屬性的set方法上。

 

public static class PropertyValueTestBean

 

    private String defaultLocale;

 

    @Value("#{ systemProperties['user.region'] }")

    public void setDefaultLocale(String defaultLocale) {

        this.defaultLocale = defaultLocale;

    }

 

    public String getDefaultLocale() {

        return this.defaultLocale;

    }

 

}

 

Autowired methods and constructors can also use the @Value annotation.

使用@Autowired方法在構造方法中也可以使用@Value注解

 

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;

    }

 

    // ...

}

 

public class MovieRecommender {

 

    private String defaultLocale;

 

    private CustomerPreferenceDao customerPreferenceDao;

 

    @Autowired

    public MovieRecommender(CustomerPreferenceDao customerPreferenceDao,

            @Value("#{systemProperties['user.country']}") String defaultLocale) {

        this.customerPreferenceDao = customerPreferenceDao;

        this.defaultLocale = defaultLocale;

    }

 

    // ...

}

 

10.5 Language Reference

語言參考

 

10.5.1 Literal expressions

輕量級表達式

 

The types of literal expressions supported are strings, dates, numeric values (int, real, and hex), boolean and null. Strings are delimited by single quotes. To put a single quote itself in a string use two single quote characters. The following listing shows simple usage of literals. Typically they would not be used in isolation like this, but as part of a more complex expression, for example using a literal on one side of a logical comparison operator.

輕量級表達式類型支持字符串、日期、數值(int、real和hex)、布爾值和null。字符串使用單引號定義。為了加入單引號需要使用兩個雙引號字符。下面的列子展示了使用方法。通常不會孤立的被使用如下,但是作為一個復雜表達式的一部分,例如使用輕量級作為邏輯比較符的一邊。

 

ExpressionParser parser = new SpelExpressionParser();

 

// evals to "Hello World"

// 與“Hello World”相同

String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();

 

double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();

 

// evals to 2147483647

// 與2147483647相同

int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();

 

boolean trueValue = (Boolean) parser.parseExpression("true").getValue();

 

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

 

Numbers support the use of the negative sign, exponential notation, and decimal points. By default real numbers are parsed using Double.parseDouble().

數值支持使用負號、指數記法和小數點。默認數值會通過Double.parseDouble()來解析。

 

10.5.2 Properties, Arrays, Lists, Maps, Indexers

Properties、數組、List、Map和索引器

 

Navigating with property references is easy: just use a period to indicate a nested property value. The instances of the Inventor class, pupin, and tesla, were populated with data listed in the section Classes used in the examples. To navigate "down" and get Tesla’s year of birth and Pupin’s city of birth the following expressions are used.

調用屬性的引用是簡單的:只要指定內置的屬性值就可以。Inventor類的實例、pupin和tesla列在10.6節中。下面的表達式用于獲得Tesla的出生年和Pupin的出生城市。

 

// evals to 1856

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

 

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

 

Case insensitivity is allowed for the first letter of property names. The contents of arrays and lists are obtained using square bracket notation.

對于屬性名的第一個字母的大小寫不是必須的要求。對于數組和list的內容可以使用方括號下標的形式。

 

ExpressionParser parser = new SpelExpressionParser();

 

// Inventions Array

StandardEvaluationContext teslaContext = new StandardEvaluationContext(tesla);

 

// evaluates to "Induction motor"

String invention = parser.parseExpression("inventions[3]").getValue(

        teslaContext, String.class);

 

// Members List

StandardEvaluationContext societyContext = new StandardEvaluationContext(ieee);

 

// evaluates to "Nikola Tesla"

String name = parser.parseExpression("Members[0].Name").getValue(

        societyContext, String.class);

 

// List and Array navigation

// evaluates to "Wireless communication"

String invention = parser.parseExpression("Members[0].Inventions[6]").getValue(

        societyContext, String.class);

 

The contents of maps are obtained by specifying the literal key value within the brackets. In this case, because keys for the Officers map are strings, we can specify string literals.

maps的內容通過方括號包圍的文字的key值定義。因此,因為Officers的keys是字符串,我們可以定義字符字面值。

 

// 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");

 

10.5.3 Inline lists

內部的list

 

Lists can be expressed directly in an expression using {} notation.

list可以直接通過{}來表示。

 

// evaluates to a Java list containing the four numbers

// 等同于定義了一個有四個元素的java的list

List numbers = (List) parser.parseExpression("{1,2,3,4}").getValue(context);

 

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

 

{} by itself means an empty list. For performance reasons, if the list is itself entirely composed of fixed literals then a constant list is created to represent the expression, rather than building a new list on each evaluation.

{}本身意味著一個空的list。處于性能的考慮,如果list本身是由固定的字面值,一個常量的list可以通過一個表達式來創建,而不是每次創建一個新的list。

 

10.5.4 Inline Maps

內聯的maps

 

Maps can also be expressed directly in an expression using {key:value} notation.

maps也可以直接使用{key:value}的形式通過表達式來定義。

 

// evaluates to a Java map containing the two entries

// 相當于一個包含兩個元素的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);

 

{:} by itself means an empty map. For performance reasons, if the map is itself composed of fixed literals or other nested constant structures (lists or maps) then a constant map is created to represent the expression, rather than building a new map on each evaluation. Quoting of the map keys is optional, the examples above are not using quoted keys.

{:}本身是一個空的map。出于性能的考慮,如果map自身是由固定的字面量或其他內嵌的常量結構(list或map)最好通過表達式創建,比每次創建一個新的map要好。map的雙引號是可選的,例子中沒有使用雙引號的key。

 

10.5.5 Array construction

數組的構造

 

Arrays can be built using the familiar Java syntax, optionally supplying an initializer to have the array populated at construction time.

數組可以通過使用類似的java語法來創建,可以在構造時提供一個創建數組的初始化器。

 

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);

 

It is not currently allowed to supply an initializer when constructing a multi-dimensional array.

目前不支持創建多維數組的初始化器。

 

10.5.6 Methods

方法

 

Methods are invoked using typical Java programming syntax. You may also invoke methods on literals. Varargs are also supported.

通過java語法可以調用方法。你也可以以字面的方式調用方法。支持可變參數。

 

// string literal, evaluates to "bc"

// 字符串字面量,相當于'bc'

String c = parser.parseExpression("'abc'.substring(2, 3)").getValue(String.class);

 

// evaluates to true

boolean isMember = parser.parseExpression("isMember('Mihajlo Pupin')").getValue(

        societyContext, Boolean.class);

 

10.5.7 Operators

操作符

 

Relational operators

關系操作符

 

The relational operators; equal, not equal, less than, less than or equal, greater than, and greater than or equal are supported using standard operator notation.

關系操作符:相等、不等、小于、小于等于、大于、大于等于可以通過使用標準的操作符被支持。

 

// 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);

 

In addition to standard relational operators SpEL supports the instanceof and regular expression based matches operator.

除了標準的關系操作符外,SpEL支持instanceof和正則表達式基于matchs操作符。

 

// 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);

 

[Note]

注意

 

Be careful with primitive types as they are immediately boxed up to the wrapper type, so 1 instanceof T(int) evaluates to false while 1 instanceof T(Integer) evaluates to true, as expected.

使用原始類型的時候小心他們直接被包裝成包裝類,因此1 instanceof T(int)是false的而1 instanceof T(Integer)是true。

 

Each symbolic operator can also be specified as a purely alphabetic equivalent. This avoids problems where the symbols used have special meaning for the document type in which the expression is embedded (eg. an XML document). The textual equivalents are shown here: lt (<), gt (>), le (?), ge (>=), eq (==), ne (!=), div (/), mod (%), not (!). These are case insensitive.

每一個符號操作符可以使用直接的單詞首字母來定義。這樣可以避免在某些特定的表達式會在文件類型中出現問題(比如xml)。現在列出文本的替換規則:lt (<), gt (>), le (?), ge (>=), eq (==), ne (!=), div (/), mod (%), not (!)。他們是不區分大小寫的。

 

Logical operators

邏輯操作符

 

The logical operators that are supported are and, or, and not. Their use is demonstrated below.

支持邏輯操作符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);

 

Mathematical operators

算術操作符

 

The addition operator can be used on both numbers and strings. Subtraction, multiplication and division can be used only on numbers. Other mathematical operators supported are modulus (%) and exponential power (^). Standard operator precedence is enforced. These operators are demonstrated below.

加法可以用在數值和字符串之間。減法、乘法和除法只能用在數值之間。其他算術操作符支持取余和乘方。標準的運算符是支持優先級的。例子如下:

 

// Addition

int two = parser.parseExpression("1 + 1").getValue(Integer.class); // 2

 

String testString = parser.parseExpression(

        "'test' + ' ' + 'string'").getValue(String.class); // 'test string'

 

// Subtraction

int four = parser.parseExpression("1 - -3").getValue(Integer.class); // 4

 

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

 

// Multiplication

int six = parser.parseExpression("-2 * -3").getValue(Integer.class); // 6

 

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

 

// Division

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

 

10.5.8 Assignment

分配

 

Setting of a property is done by using the assignment operator. This would typically be done within a call to setValue but can also be done inside a call to getValue.

屬性的設置是通過賦值操作符。通常會叫做設置值也可以getValue來實現。

 

Inventor inventor = new Inventor();

StandardEvaluationContext inventorContext = new StandardEvaluationContext(inventor);

 

parser.parseExpression("Name").setValue(inventorContext, "Alexander SEOvic2");

 

// alternatively

// 或者使用下面的方法

 

String aleks = parser.parseExpression(

        "Name = 'Alexandar Seovic'").getValue(inventorContext, String.class);

 

10.5.9 Types

類型

 

The special T operator can be used to specify an instance of java.lang.Class (the type). Static methods are invoked using this operator as well. The StandardEvaluationContext uses a TypeLocator to find types and the StandardTypeLocator (which can be replaced) is built with an understanding of the java.lang package. This means T() references to types within java.lang do not need to be fully qualified, but all other type references must be.

特殊的T操作符用于定義一個類的實例。也可以使用該操作符調用靜態方法。StandardEvaluationContext使用TypeLocator來查找類型和創建StandardTypeLocator(被替換)來累計額java.lang包。這意味著T代表java.lang的類型不需要類的全限定名,但是其他的類型引用需要。

 

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);

 

10.5.10 Constructors

構造器

 

Constructors can be invoked using the new operator. The fully qualified class name should be used for all but the primitive type and String (where int, float, etc, can be used).

構造器可以使用新的操作符來調用。除了基本類型和String外需要使用全限定類名(int、float等等是可以直接使用的)

 

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);

 

10.5.11 Variables

變量

 

Variables can be referenced in the expression using the syntax #variableName. Variables are set using the method setVariable on the StandardEvaluationContext.

變量可以在表達式中通過#variableName來表示。變量的設置使用StandardEvaluationContext的setVariable方法。

 

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

StandardEvaluationContext context = new StandardEvaluationContext(tesla);

context.setVariable("newName", "Mike Tesla");

 

parser.parseExpression("Name = #newName").getValue(context);

 

System.out.println(tesla.getName()) // "Mike Tesla"

 

The #this and #root variables

#this和#root變量

 

The variable #this is always defined and refers to the current evaluation object (against which unqualified references are resolved). The variable #root is always defined and refers to the root context object. Although #this may vary as components of an expression are evaluated, #root always refers to the root.

#this變量始終指向當前的object(處理沒有全限定的引用)。#root變量使用指向根上下文object。盡管#this可能根據表達式而不同,但是#root一直指向根。

 

// create an array of integers

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();

StandardEvaluationContext context = new StandardEvaluationContext();

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);

 

10.5.12 Functions

函數

 

You can extend SpEL by registering user defined functions that can be called within the expression string. The function is registered with the StandardEvaluationContext using the method.

你可以通過用戶自定義函數來擴展SpEL可以在表達式字符串中使用。函數使用StandardEvaluationContext的方法來注冊。

 

public void registerFunction(String name, Method m)

 

A reference to a Java Method provides the implementation of the function. For example, a utility method to reverse a string is shown below.

java方法的引用提供了函數的實現。例如,一個翻轉字符串的方法實現如下。

 

public abstract class StringUtils {

 

    public static String reverseString(String input) {

        StringBuilder backwards = new StringBuilder();

        for (int i = 0; i < input.length(); i++)

            backwards.append(input.charAt(input.length() - 1 - i));

        }

        return backwards.toString();

    }

}

 

This method is then registered with the evaluation context and can be used within an expression string.

這個方法注冊在上下文中可以如下使用。

 

ExpressionParser parser = new SpelExpressionParser();

StandardEvaluationContext context = new StandardEvaluationContext();

 

context.registerFunction("reverseString",

    StringUtils.class.getDeclaredMethod("reverseString", new Class[] { String.class }));

 

String helloWorldReversed = parser.parseExpression(

    "#reverseString('hello')").getValue(context, String.class);

 

10.5.13 Bean references

bean的引用

 

If the evaluation context has been configured with a bean resolver it is possible to lookup beans from an expression using the (@) symbol.

如果評價上下文通過一個bean的解析器被配置,就可以從使用@標識符的表達式中查找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

// 這樣會在處理時調用MyBeanResolver的resolve(context,"foo")

Object bean = parser.parseExpression("@foo").getValue(context);

 

To access a factory bean itself, the bean name should instead be prefixed with a (&) symbol.

為了訪問工廠bean本身,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

// 這樣會在處理時調用MyBeanResolver的resolve(context,"&foo")

Object bean = parser.parseExpression("&foo").getValue(context);

 

10.5.14 Ternary Operator (If-Then-Else)

三元操作符(If-Then-Else)

 

You can use the ternary operator for performing if-then-else conditional logic inside the expression. A minimal example is:

你可以在表達式中使用三元操作符實現if-then-else的邏輯。一個小例子如下:

 

String falseString = parser.parseExpression(

        "false ? 'trueExp' : 'falseExp'").getValue(String.class);

 

In this case, the boolean false results in returning the string value 'falseExp'. A more realistic example is shown below.

在這個例子中,布爾的false值將返回'falseExp'字符串。一個比較復雜的例子如下。

 

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"

 

Also see the next section on the Elvis operator for an even shorter syntax for the ternary operator.

參照下一節可以使用Elvis操作符實現更加簡短的三元操作符語法。

 

10.5.15 The Elvis Operator

Elvis操作符

 

The Elvis operator is a shortening of the ternary operator syntax and is used in the Groovy language. With the ternary operator syntax you usually have to repeat a variable twice, for example:

Elvis操作符是在Groovy語言中使用的比較簡短的三元操作符。使用三元操作符你可以重復一個變量兩次,如下:

 

String name = "Elvis Presley";

String displayName = name != null ? name : "Unknown";

 

Instead you can use the Elvis operator, named for the resemblance to Elvis' hair style.

你可以使用Elvis操作符來實現,上面例子的也可以如下的形式展現。

 

ExpressionParser parser = new SpelExpressionParser();

 

String name = parser.parseExpression("name?:'Unknown'").getValue(String.class);

 

System.out.println(name); // 'Unknown'

 

Here is a more complex example.

下面是一個比較復雜的例子。

 

ExpressionParser parser = new SpelExpressionParser();

 

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

StandardEvaluationContext context = new StandardEvaluationContext(tesla);

 

String name = parser.parseExpression("Name?:'Elvis Presley'").getValue(context, String.class);

 

System.out.println(name); // Nikola Tesla

 

tesla.setName(null);

 

name = parser.parseExpression("Name?:'Elvis Presley'").getValue(context, String.class);

 

System.out.println(name); // Elvis Presley

 

10.5.16 Safe Navigation operator

安全的導航操作符

 

The Safe Navigation operator is used to avoid a NullPointerException and comes from the Groovy language. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return null instead of throwing an exception.

安全的導航操作符用于避免空指針異常并且來自Groovy語言。通常當你指向一個object你希望判斷是否為空在訪問方法或屬性之前。為了避免如此,安全的導航操作符將簡單返回null而不是拋出一個異常。

 

ExpressionParser parser = new SpelExpressionParser();

 

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

tesla.setPlaceOfBirth(new PlaceOfBirth("Smiljan"));

 

StandardEvaluationContext context = new StandardEvaluationContext(tesla);

 

String city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);

System.out.println(city); // Smiljan

 

tesla.setPlaceOfBirth(null);

 

city = parser.parseExpression("PlaceOfBirth?.City").getValue(context, String.class);

 

System.out.println(city); // null - does not throw NullPointerException!!!

 

[Note]

注意

 

The Elvis operator can be used to apply default values in expressions, e.g. in an @Value expression:

Elvis操作符可以在表達式中應用于默認值,例如,在@Value表達式中:

 

@Value("#{systemProperties['pop3.port'] ?: 25}")

 

This will inject a system property pop3.port if it is defined or 25 if not.

這樣會調用系統參數pop3.port,如果沒有定義將會返回25。

 

10.5.17 Collection Selection

集合的選擇

 

Selection is a powerful expression language feature that allows you to transform some source collection into another by selecting from its entries.

選擇是一個強大的表達式語言特性允許你將一些源數據集合轉換為另一個集合通過選擇他們的條目。

 

Selection uses the syntax ?[selectionExpression]. This will filter the collection and return a new collection containing a subset of the original elements. For example, selection would allow us to easily get a list of Serbian inventors:

選擇使用語法是?[selectionExpression]。這將會過濾集合并返回一個新的集合包含一個原始數據的子集合。例如,選擇應當允許我們簡單獲得一個Serbian inventors的list:

 

List<Inventor> list = (List<Inventor>) parser.parseExpression(

        "Members.?[Nationality == 'Serbian']").getValue(societyContext);

 

Selection is possible upon both lists and maps. In the former case the selection criteria is evaluated against each individual list element whilst against a map the selection criteria is evaluated against each map entry (objects of the Java type Map.Entry). Map entries have their key and value accessible as properties for use in the selection.

選擇可以使用在list和map上。在前面的例子中選擇獨立處理了集合中的元素,而當選擇一個map時將會處理每個map的entry(Java類型Map.Entry的object)。Map的entry有他的key和value作為屬性訪問使用在選擇中。

 

This expression will return a new map consisting of those elements of the original map where the entry value is less than 27.

表達式將返回一個新的map包括原有map中所有的value小于27的條目。

 

Map newMap = parser.parseExpression("map.?[value<27]").getValue();

 

In addition to returning all the selected elements, it is possible to retrieve just the first or the last value. To obtain the first entry matching the selection the syntax is ^[…?] whilst to obtain the last matching selection the syntax is $[…?].

額外為了返回所有選擇的元素,可以獲得第一個值或最后一個值。為了獲得第一個條目在選擇的語法是^[…?]而獲得最后一個值的語法是$[…?]。

 

10.5.18 Collection Projection

集合投影

 

Projection allows a collection to drive the evaluation of a sub-expression and the result is a new collection. The syntax for projection is ![projectionExpression]. Most easily understood by example, suppose we have a list of inventors but want the list of cities where they were born. Effectively we want to evaluate 'placeOfBirth.city' for every entry in the inventor list. Using projection:

投影允許一個集合被一個子表達式處理而且結果是一個新的集合。投影的語法是![projectionExpression]。通過例子可便于理解,假設我們有一個invertors的list并且希望他們出生的cities的list。有效的做法是我們希望對每個在invertor的list調用'placeOfBirth.city'。使用投影:

 

// returns ['Smiljan', 'Idvor' ]

List placesOfBirth = (List)parser.parseExpression("Members.![placeOfBirth.city]");

 

A map can also be used to drive projection and in this case the projection expression is evaluated against each entry in the map (represented as a Java Map.Entry). The result of a projection across a map is a list consisting of the evaluation of the projection expression against each map entry.

一個map可以用于處理投影并且在這種情況下投影表達式可以對map中的每個entry進行處理(作為一個Java的Map.Entry)。投影的結果跨越map是一個list包含對每一個map條目處理的投影表達式。

 

10.5.19 Expression templating

表達式模板

 

Expression templates allow a mixing of literal text with one or more evaluation blocks. Each evaluation block is delimited with prefix and suffix characters that you can define, a common choice is to use #{ } as the delimiters. For example,

表達式模板允許一個混合的文本有一個或多個評價塊。每個評價塊用前綴和后綴區分你可以定義一個通用的選項通過使用#{}作為一個分隔符。例如,

 

String randomPhrase = parser.parseExpression(

        "random number is #{T(java.lang.Math).random()}",

        new TemplateParserContext()).getValue(String.class);

 

// evaluates to "random number is 0.7038186818312008"

 

The string is evaluated by concatenating the literal text 'random number is ' with the result of evaluating the expression inside the #{ } delimiter, in this case the result of calling that random() method. The second argument to the method parseExpression() is of the type ParserContext. The ParserContext interface is used to influence how the expression is parsed in order to support the expression templating functionality. The definition of TemplateParserContext is shown below.

字符串包含一個文本'random number is '和在#{}中的表達式的處理結果,在這個例子中結果調用了random方法。第二個參數對于parseExpression方法是ParseContext的類型。ParserContext接口被使用來影響表達式是如何被解析來支持表達式的模板。定義TemplateParserContext的方法如下。

 

public class TemplateParserContext implements ParserContext {

 

    public String getExpressionPrefix() {

        return "#{";

    }

 

    public String getExpressionSuffix() {

        return "}";

    }

 

    public boolean isTemplate() {

        return true;

    }

}

 

10.6 Classes used in the examples

 

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;

    }

 

}

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久三级免费电影 | 日韩免费黄色 | 在线成人精品视频 | 亚洲日本欧美 | 国产精品一区二区三区在线播放 | 久草成人在线 | 92看片淫黄大片欧美看国产片 | 欧美成人午夜一区二区三区 | 精品一区二区三区四区在线 | 亚洲3atv精品一区二区三区 | 加勒比色综合 | 欧美中文字幕一区二区三区亚洲 | 黄色av片三级三级三级免费看 | mmmwww| 特级a欧美做爰片毛片 | 久久av免费 | 国产精品91久久久 | 中文字幕国 | 欧美精品成人一区二区三区四区 | 亚洲成人激情在线 | 亚洲一区二区三区精品在线观看 | 毛片大全免费看 | 毛片毛片免费看 | h视频免费看| 日韩欧美高清一区 | 欧美日韩亚州综合 | 香蕉久久久精品 | 国产精品一区二区三区99 | 亚洲看片网 | 99sesese| 久久影院免费观看 | 毛片视频网址 | 日韩欧美高清一区 | 性爱视频在线免费 | 国产又白又嫩又紧又爽18p | 国产1区在线观看 | 国产伦精品一区二区三区 | 一级在线| 精品国产91久久久久久浪潮蜜月 | 全黄毛片 | 国产精品久久久久久影视 |