In this post under Spring SpEL, I will show with example the purpose of “autoGrowNullReferences” configuration.
When evaluating a SpEL expression, containing chain of property references, if one of the property in the chain is null we get an exception.
To prevent that we use the “autoGrowNullReferences” configuration. It’s data type is boolean. By default it is false.
When enabled, if any property in the chain of property references is null, SpEL create an instance of that property using property data type’s no argument constructor.
Lets say we have the below pojo class
Employee
package spel.package21;
public class Employee {
private Integer id;
private String name;
private int salary;
//Removed getter and setter for brevity.
}
Now when we execute a below SpEL expression
employee.name.empty
And if “employee” instance is null. We get the below exception
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
To prevent this exception from happening, we can take help of “autoGrowNullReferences” configuration.
If “autoGrowNullReferences” value is true, SpEL creates an instance for “employee” and the subsequent “name” property and the output of the expression will be “true”
Below is the complete main class showing how to use “autoGrowNullReferences”.
Main class
package spel.package21;
import org.springframework.expression.Expression;
import org.springframework.expression.spel.SpelParserConfiguration;
import org.springframework.expression.spel.standard.SpelExpressionParser;
public class Example21 {
public Employee employee;
public static void main(String[] args) {
SpelExpressionParser expressionParser = new SpelExpressionParser();
Expression expression = expressionParser.parseExpression("employee.name.empty");
Example21 example21 = new Example21();
try {
System.out.println(expression.getValue(example21, Boolean.class));
} catch(Exception exception) {
System.out.println(exception);
}
SpelParserConfiguration spelParserConfiguration = new SpelParserConfiguration(true, false);
expressionParser = new SpelExpressionParser(spelParserConfiguration);
expression = expressionParser.parseExpression("employee.name.empty");
example21 = new Example21();
System.out.println(expression.getValue(example21, Boolean.class));
}
}
In the above code at line 11, I create an instance of “SpelExpressionParser” class.
At line 12, I parse the expression.
At line 15, I evaluate the expression and print the result.
In this scenario since we haven’t set “autoGrowNullReferences” to true we will an exception and it is catched and printed to the console.
Next at line 20, I create an instance of “SpelParserConfiguration” and pass two constructor arguments one for “autoGrowNullReferences” and another for “autoGrowCollections”.
So for “autoGrowNullReferences” we pass “true” and for “autoGrowCollections” we pass “false”.
We will discuss about “autoGrowCollections” in future posts for now we will keep our focus to constructor’s first argument which is “autoGrowNullReferences”.
At line 24, when we evaluate the expression again.
This time SpEL will create an instance of “employee (which is a Employee class)” and “name (which is a String class)” and print the output as “true”.
In this way we can use “autoGrowNullReferences” configuration.
Below is the output
Output
org.springframework.expression.spel.SpelEvaluationException: EL1007E: Property or field 'name' cannot be found on null
true