Beginner's Guide to Spring Expression Language With Spring Boot

DZone 's Guide to

Beginner's Guide to Spring Expression Language With Spring Boot

Spring into action with SpEL!

· Java Zone ·
Free Resource


Spring into action with SpEL!

Spring Expression Language (SpEL) is a powerful expression language, which can be used for querying and manipulating an object graph at runtime. SpEL is available via XML or annotations and is evaluated during the bean creation time.

In this article, we will look at some basic examples of SpEL usage in Spring Boot.

You may also like:  Learning the Spring Expression Language (SpEL)

Setting up Spring Boot Application

We will create a simple Spring Boot application and create the employee.properties file in the resources directory.

employee.names=Petey Cruiser,Anna Sthesia,Paul Molive,Buck Kinnear
employee.age={one:'26', two : '34', three : '32', four: '25'}

Create a class EmployeeConfig as follows:

package com.techshard.spel;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@PropertySource (name = "employeeProperties", value = "employee.properties")
public class EmployeeConfig {

Reading Configuration Using SpEL and @Value Annotation

We will add some fields to read the configuration from employee.properties using @Value annotation. The @Value annotation can be used for injecting values into fields in Spring-managed beans, and it can be applied at the field, constructor, or method parameter level.

@Value ("#{'${employee.names}'.split(',')}")
private List<String> employeeNames;

Here, we are using Spring expression language to get a list of employee names. We can manipulate the properties to get the list of values. The field employeeNames will give a list: [Petey Cruiser, Anna Sthesia, Paul Molive, Buck Kinnear].

Suppose we want to get only first entry from a list of employee names; we can write the expression as follows:

@Value ("#{'${employee.names}'.split(',')[0]}")
 private String firstEmployeeName;

This will get the first name from the list: Petey Cruiser.

Let us now look at how to use @Value with  Maps. We have the following property defined.

employee.age={one:'26', two : '34', three : '32', four: '25'}

We can inject the above the property value as a Map as follows:

@Value ("#{${employee.age}}")
 private Map<String, Integer> employeeAge;

Suppose we want to get the value using certain key; we can get the value as follows:

@Value ("#{${employee.age}.two}")
private String employeeAgeTwo;

If we are not sure if a certain key exists and we want to get a default value, then we could write the expression as follows:

@Value ("#{${employee.age}['five'] ?: 30}") 
private Integer ageWithDefaultValue;

This would give the value as 30 if the key 'five' does not exist.

Reading System Properties Using @Value Annotation

We can also inject system properties using SpEL. For example, Java home can be injected as follows:

@Value ("#{systemProperties['java.home']}") 
private String javaHome;

Similarly, the user directory can be injected as below:

@Value ("#{systemProperties['user.dir']}") 
private String userDir;

Wrapping Up

In this article, we looked at various possibilities of using SpEL with the @Value annotation. If you wish to learn more about Spring Expression Language, read the Spring documentation here.

A complete example can be found on this GitHub repository.

Further Reading

Learning the Spring Expression Language (SpEL)

Evaluating Expressions Using the Spring Expression Language

annotations, introduction, java, spel, spring, spring expression language, xml

Published at DZone with permission of Swathi Prasad , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}