DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Trending

  • Why AI-Generated Code Breaks Your Testing Assumptions
  • Why Pass/Fail CI Pipelines Are Insufficient for Enterprise Release Decisions
  • Key Takeaways From Integrating a RAG Application With LangSmith
  • Why We Chose Iceberg Over Delta After Evaluating Both at Scale
  1. DZone
  2. Coding
  3. Frameworks
  4. Beginner's Guide to Spring Expression Language With Spring Boot

Beginner's Guide to Spring Expression Language With Spring Boot

Spring into action with SpEL!

By 
Swathi Prasad user avatar
Swathi Prasad
·
Oct. 08, 19 · Presentation
Likes (19)
Comment
Save
Tweet
Share
58.9K Views

Join the DZone community and get the full member experience.

Join For Free

Spring!

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.type=contract,fulltime,external
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;

@Configuration
@PropertySource (name = "employeeProperties", value = "employee.properties")
@ConfigurationProperties
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

Spring Framework Spring Boot

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

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • How Spring Boot Starters Integrate With Your Project
  • A Practical Guide to Creating a Spring Modulith Project
  • Structured Logging in Spring Boot 3.4 for Improved Logs

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook