5.1 Overview 5.1.1 Overview
Spring expression language is all called "spring expression Language", abbreviated as "spel", similar to the OGNL expression language used in struts2x, can build complex expressions at Run time, Access object Graph properties, Object method calls, etc. And can be perfectly integrated with spring functionality, such as the ability to configure Bean Definitions.
The expression language adds dynamic functionality to the static Java Language.
Spel is a standalone module that relies only on the core module and does not depend on other modules and can be used ALONE.
What can 5.1.2 do?
Expression language is generally the simplest form of accomplishing the most important work, reducing our WORKLOAD.
Spel supports the following expressions:
basic expression: literal expression, relation, logic and arithmetic expression, string connection and intercept expression, trinocular operation and Elivis expression, regular expression, bracket precedence expression;
class -related expressions: class-type expressions, class instantiation, instanceof expressions, variable definitions and references, assignment expressions, custom functions, object property access and safe Navigation expressions, object Method calls, Bean references;
three, Set related expressions: inline list, inline array, collection, dictionary access, list, dictionary, array modification, set projection, collection selection; no support for multidimensional inline array initialization; inline dictionary definitions are not supported;
Iv. Other Expressions : template Expressions.
note: Spel the keywords in an expression are Case-insensitive.
5.2 Spel Basic 5.2.1 HelloWorld
First prepare the jar package that supports spel: "org.springframework.expression-3.0.5.release.jar" to add it to the Classpath.
Spel is generally divided into four steps in the expression value, the third step is optional: first constructs a parser, followed by the parser parse the string expression, in this construction context, finally according to the context to get the value of the expression after the Operation.
Let's take a look at the code snippet:
packagecn.javass.spring.chapter5; Importjunit.framework.Assert; Importorg.junit.Test; Importorg.springframework.expression.EvaluationContext; Importorg.springframework.expression.Expression; Importorg.springframework.expression.ExpressionParser; Importorg.springframework.expression.spel.standard.SpelExpressionParser; Importorg.springframework.expression.spel.support.StandardEvaluationContext; public classspeltest {@Test public voidhelloWorld () {expressionparser parser=NewSpelexpressionparser (); Expression Expression=Parser.parseexpression ("(' Hello ' + ' World '). concat (#end)"); Evaluationcontext Context=NewStandardevaluationcontext (); Context.setvariable ("end", "!"); Assert.assertequals ("Hello world!", Expression.getvalue (context)); } }
next, Let's analyze the code:
1) Create Parser: Spel uses ExpressionParser interface to represent the parser, providing spelexpressionparser default implementation;
2) Parse expression: use Expressionparser's parseexpression to parse the corresponding expression as an expressions Object.
3) construct Context: Prepare the contextual data required for expressions such as variable definitions, and so On.
4) evaluation: The expression value is derived from the context using the GetValue method of the expression Interface.
is not very simple, then let us look at its specific implementation and principles of it.
5.2.3 Spel principle and interface
Spel provides a simple interface that simplifies user use, and lets us learn the next few concepts before introducing the Principle:
expression : expression is the core of the expression language, so the expression language is all around the expression, from our point of view is "doing";
second, parser: used to parse a string expression into an expression object, from our point of view is "who to do";
iii. context: an environment in which an expression object executes, which may define variables, define custom functions, provide type conversions, and so on, from our point of view is "where to do";
the root object and the activity context object: The root object is the default active context object, and the activity context object represents the object of the current expression operation, from our point of view is "who to do".
After understanding these concepts, let's look at how Spel works, as shown in 5-1:
Figure 5-1 How it works
1) first define an expression: "1+2";
2) define the parser ExpressionParser implementation, Spel provide the default implementation spelexpressionparser;
2.1) Spelexpressionparser parser uses Tokenizer class for lexical analysis, that is, the string stream is analyzed as a tick stream, and the notation is represented by token class in spel;
2.2) with a tick stream, the parser can generate an internal abstract syntax tree based on the tick flow; in spel, The syntax tree node is represented by the Spelnode interface: for example, opplus represents an int literal node, and the intliteral represents the ; using Spelnodel to make up the abstract syntax tree;
2.3) provide an expression interface to simplify the representation of the abstract syntax tree, thus hiding the internal implementation details, and provide GetValue simple method to get the expression value; Spel provides a default implementation of spelexpression;
3) define the expression context object (optional), Spel uses the Evaluationcontext interface to represent the context object, which is used to set the root object, custom variables, custom functions, type converters, etc. SPEL provides default implementation standardevaluationcontext;
4) use an Expression object to obtain the result based on the context object (optional) evaluation (called The GetValue method of the expression object).
next, Let's look at the main interface of Spel:
1 ) ExpressionParser interface : Represents the parser, The default implementation is the Spelexpressionparser class in the Org.springframework.expression.spel.standard package, which uses the ParseExpression method to convert a string expression to an expression object, for par The Sercontext interface is used to define whether a string expression is a template, and the template starts with the end Character:
public Interface expressionparser { Expression parseexpression (String expressionstring); Expression parseexpression (String expressionstring, parsercontext context); }
Take a look at the example:
@Test public voidtestparsercontext () {expressionparser parser=NewSpelexpressionparser (); ParserContext ParserContext=Newparsercontext () {@Override public Booleanistemplate () {return true; } @Override publicString getexpressionprefix () {return"#{"; } @Override publicString getexpressionsuffix () {return"}"; } }; String Template= "#{' Hello '}#{' world! '}"; Expression Expression=parser.parseexpression (template, parsercontext); Assert.assertequals ("Hello world!", Expression.getvalue ()); }
Here we demonstrate the use of parsercontext, where the ParserContext implementation is defined: the definition expression is a module, the expression prefix is "#{", the suffix is "}", and the template passed in with ParseExpression parsing must start with "#{". End with "}", such as "#{' Hello '}#{' world! '}".
The default passed-in string expression is not a template form, such as Hello world, which was previously Demonstrated.
2 ) Evaluationcontext interface : Represents the context environment, The default implementation is the Standardevaluationcontext class in the Org.springframework.expression.spel.support package, using the Setrootobject method to set the root object, using the SetVariable method to register the Define variables, use Registerfunction to register custom functions, and so On.
3 ) Expression Interface: represents an expression object, The default implementation is spelexpression in the Org.springframework.expression.spel.standard package, which provides getvalue methods for getting expression values, and provides SetValue methods for setting object Values.
Understanding the Spel principle and interface, The next thing is Spel grammar.
Spring Expression Language 5.1 Overview 5.2 Spel Foundation (three)