Spring Expression Language Guide For Beginner's

Spring Expression Language Guide For Beginner's

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

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.

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.

Originally published by Swathi Prasad at dzone.com


Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Spring Boot Tutorials - Spring Boot Full Course

Spring Boot Tutorials | Full Course - What is Spring? Spring Boot is an open source Java-based framework used to create a Micro Service. Spring Boot contains a comprehensive infrastructure support for developing a micro service and enables you to develop enterprise-ready applications that you can “just run”.

Spring vs Spring Boot

We will see how Spring and Spring Boot can be compared. Programmers need not start from scratch when they want to work on some projects.

What Is Spring Boot ?

What Is Spring Boot ? I will explain you what is sprint boot. This is introduction to spring boot and why spring boot is used.

Learn Hibernate and Spring for Beginners (Includes Spring Boot)

Spring 5: Learn Spring 5 Core, AOP, Spring MVC, Spring Security, Spring REST, Spring Boot 2, Thymeleaf, JPA & Hibernate

Spring Boot - Spring Security + JWT Complete Tutorial With Example

Learn how to implement spring security using JWT (JSON Web Token). What is JWT? Why JWT? JSON Web Token (JWT) is a compact URL-safe means of representing claims to be transferred between two parties. Spring Boot - Spring Security + JWT Complete Tutorial With Example