My lack of knowledge of SpEL pays off these days when I am trying Spring Integration in the project. So I decided to learn more about it. I need to following information about SpEL.
- Features
- Syntax
- Performance
Note that the following information is some kind of note for myself. Please don't waste time on those unorganized information. Thanks.
Introduction
- The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime.
Does it mean that SpEL is not a general purpose Expression Language? I am not familiar with any expression language either. So I can't tell what the different is here.
- The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality.
- The Java Unified Expression Language is a special purpose programming language mostly used in Java web applications for embedding expressions into web pages. The Java specification writers and expert groups of the Java web-tier technologies have worked on a unified expression language which is now part of the JSP 2.1 specification (JSR-245).
- The expression language started out as part of the JavaServer Pages Standard Tag Library (JSTL) and was originally called SPEL (Simplest Possible Expression Language), then just Expression Language (EL). It was a scripting language which allowed access to Java components (JavaBeans) through JSP. Since JSP 2.0, it has been used inside JSP tags to separate Java code from JSP, and to allow easier access to Java components (than in Java code).
It turns out the Unified EL is something I knew but not am not good at.
- Features of Unified EL
- The new unified EL is a union of the JSP and JSF expression languages. In addition to the features already available in the JSP EL, the unified EL has the following features:
- Deferred evaluation
- Support for expressions that can set values and expressions that can invoke methods
- A pluggable API for resolving expressions
It seems that Unified EL can also invoke method. SpEL says it differs from Unified EL on invoking method may be not true. But anyway, let's live with it now.
Feature Overview
The expression language supports the following functionality
- Literal expressions
- Boolean and relational operators
- Regular expressions
- Class expressions
- Accessing properties, arrays, lists, maps
- Method invocation
- Relational operators
- Assignment
- Calling constructors
- Bean references
- Array construction
- Inline lists
- Ternary operator
- Variables
- User defined functions
- Collection projection
- Collection selection
- Templated expressions
Expression Evaluation using Spring's Expression Interface
- 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.
- 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.
The EvaluationContext interface
- The out-of-the-box implementation, StandardEvaluationContext, uses reflection to manipulate the object, caching java.lang.reflect's Method, Field, and Constructor instances for increased performance.
- setRootObject()
- setVariable()
- registerFunction()
- Register custom ConstructorResolvers, MethodResolvers, and PropertyAccessors to extend how SpEL evaluates expressions. (See JavaDoc)
- Generics Aware!!
Expression support for defining bean definitions
- In both cases the syntax to define the expression is of the form #{ <expression string> }.
Language Reference
Literal Expressions
- Supported literal expressions
- Strings
- Dates
- Numberic values
- Boolean
- null
- Strings are delimited by single quotes
- To put a single quote itself in a string use two single quote characters.
Properties, Arrays, Lists, Maps, Indexers
- Case insensitivity is allowed for the first letter of property names.
Inline lists
Array construction
Methods
- Using typical Java Programming syntax.
Operators
- Supports the 'instanceof' and regular expression based 'matches' operator.
- Each symbolic operator can also be specified as a purely alphabetic equivalent.
- lt
- gt
- le
- ge
- eq
- ne
- div
- mod
- not
Logical operators
- and
- or
- not
Mathematical operators
Assigment
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.
- 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.
Constructors
Variables
- #variableName
- setVariable()
#this and #root
- #this is always defined and refers to the current evaluation object.
- #root is always defined and refers to the root context object.
Functions
- registerFunction(String name, Method m)
Bean references
- If the evaluation context has been configured with a bean resolver it is possible to lookup beans from an expression using the (@) symbol
- @foo
No comments:
Post a Comment