SpEL introduction – part 1

SpEL is a powerful utility framework provided by spring for looking up object graph. We will take a look at how to use spEL in this post. We will divide this article into two parts.

We will first create a simple object and see how SpEL is used for reading and manipulating the object graph and then in the second part of the article, we will define a properites file. Read the properties and assign it to a object variable.

SpEL expression evaluation interface

We will take take a simple object here and see how to use the expression api’s to manipulate the object.


public class SampleProgrammer{
        public String name;
        public String occupation
        @Autowired
        SampleProgrammer(String name, String occupation) {
            this.name = name;
            this.occupation = occupation;
        }
}

Now we will create the object and see how to parse the object using SpEL.


       ...
        SampleProgrammer programmer = new SampleProgrammer("Sathish Jayapal", "Programmer");
        ExpressionParser expressionParser = new SpelExpressionParser();
        Expression nameExpression = programmer.parseExpression("name");
        Expression occupExpression = programmer.parseExpression("occupation");
     // EvaluationContext context = new StandardEvaluationContext(programmer);
        String name = (String) nameExpression.getValue(programmer);
        String occupation = (String) occupExpression.getValue(programmer);
        if (logger.isDebugEnabled()) {
            logger.debug("Context Name is " + name);
            logger.debug("Context Occupation is " + occupation);
        }

Looking at the code snippet above, there are few items that we can discuss. We create an Object of the Programmer and manipulate the object tree dynmaically. We are using the expression and its sublass SpelExpressionParser parser. There are two items in the SamplePrgorammer’s programmer object. They are name and occupation. We will define an expression for each class variable. After this we use the expression class to get the value parsed. Here is the code snippet for this :


        ...
        Expression nameExpression = expressionParser.parseExpression("name"); // Define name for expression
        ...
       String name = (String) nameExpression.getValue(programmer);
       ...
       if (logger.isDebugEnabled()) {
            logger.debug("Context Name is " + name);
        ...
        }

We will discuss the reason why this following line of code is commented out.

// EvaluationContext context = new StandardEvaluationContext(programmer);

StandardEvaluationContext uses reflection API’s to identify the constructors and methods. But this is a costly object, in our example we can quickly use the getValue method to get the value.

SpEL by default is interpreter based, all expression are interpreted for evaluation. The reason for this is to give flexibility during evaluation. But if the expression evaluation is not changing often, we can switch the expression to be compiled to JAVA Class. This switch can be done using the [SpringCompilerMode][1] enum parameter. There are few available modes to operate, it is OFF, IMMEDIATE and MIXED. By default SpEL has this in the OFF mode. Immediate mode ensures that expressions are compiled right away. MIXED mode as name suggests expressions are switched between the compile and interepreter mode.

We saw a quick introduction of SpEL and some basic operation in this post. In the next part we will use SpEL to read a properties file and evaluate values.

The sample code for this is in GitHub.