Using the EL parser spring

SpEL expression syntax parsing process on a high abstraction, abstract parser, expression, context parsing valuation (the Evaluate) context and other objects, a very elegant expression parsing logic. The main object as follows:

The class name

Explanation

ExpressionParser

Expression parser interface comprising the (Expression) parseExpression (String), (Expression) parseExpression (String, ParserContext) two interface methods

ParserContext

Parser context interface, mainly for the parser is an abstract class Token, including three methods: getExpressionPrefix, getExpressionSuffix and isTemplate, is an expression that represents what symbol from the beginning what the end of the symbol, whether as a template (and contains the literal expression) resolution. Generally keep the default.

Expression

The abstract expression, is the result of a string expression parsed representation. By expressionInstance.getValue methods for obtaining the value of the expression. May obtain an expression for the value of the current context from the context evaluation (Evaluation) by calling getValue (EvaluationContext)

EvaluationContext

Valuation context interface, only a setter method: setVariable (String, Object), by calling the method can provide context for the evaluation variables

 

 

    public  static  void main (String [] args) {
         // test SpringEL parser 
        String template = "# {# user }, good morning"; // set the text template, where # {} is an expression of the beginning and ending, # user is string expression, is a reference to a variable. 
        PASER = ExpressionParser is new new SpelExpressionParser (); // Create expression parser 
        
        // may be set in context by a variable evaluationContext.setVariable. 
        Context = EvaluationContext new new StandardEvaluationContext (); 
        context.setVariable ( "the User", "Dawn" ); 

        // analytic expression, if the expression is a template expression, the need to resolve the incoming template parser context. 
        = Paser.parseExpression expression The expression The (Template, new new TemplateParserContext ());
        
        // Use Expression.getValue () Gets the value of the expression, where the incoming Evalution context, the second parameter is a type parameter indicating the type of return value. 
        System.out.println (expression.getValue (context, String. Class )); 
    }

 

Output:

 

Guess you like

Origin www.cnblogs.com/lidedong/p/11580110.html