Concept and role
The 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 Property 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.
Support form (case insensitive):
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.
Test Example:
public class Speltest {
@Test
public void Speltest () {
ExpressionParser expressionparser = NE W Spelexpressionparser ();
ParserContext parsercontext = new ParserContext () {
@Override
public boolean IsT Emplate () {
return true;
@Override
Public String getexpressionprefix () {//prefix expression
return ' #{' ;
}
@Override
Public String getexpressionsuffix () {//suffix expression
Return "}";
}
};
String template = "#{' Hello '}#{' abl '}#{' world! '}";//expressions
Expression expression = ExpressionParser . parseexpression (template, ParserContext);
//Note must match exactly
Assert.assertequals ("Hello abl world!", Expression.getvalue ());
}
}
Note: The default incoming string expression is not a template form
2 ) Evaluationcontext interface : Represents the context environment, and the default implementation is the Standardevaluationcontext class in the Org.springframework.expression.spel.support package. Use the Setrootobject method to set the root object, use the SetVariable method to register the custom variable, use Registerfunction to register the custom function, 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.
Spel Syntax:
String str1 = parser.parseexpression ("' Hello world! '"). GetValue (String.class);
String str2 = parser.parseexpression ("\" Hello world!\ ""). GetValue (String.class);
Subtraction
int result1 = parser.parseexpression ("1+2-3*4/2"). GetValue (Integer.class);//-3
Relationship expression
Equal to (= =), not equal to (! =), greater than (>), greater than or equal (>=), less than (<), less than or equal (<=), interval (between) operation
Logical expression:
and (and), or (or), non (! or not).
Boolean result2 = Parser.parseexpression (expression2). GetValue (Boolean.class); Assert.assertequals (True, RESULT2);
string joins and intercept expressions: string connections with "+"
Trinocular operator "Expression 1? Expression 2: An expression 3 " to construct a three-mesh expression, such as" 2>1?true:false ", to return true;
Regular expression: use "str matches regex, such as" ' 123 ' matches ' \\d{3} ' "will return true;
parentheses Precedence Expressions: constructed with an "(expression)", with high precedence in parentheses.
Class-Dependent expressions
This piece of stuff is really boring, no meaning, no writing.
spring-Expression Language