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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • AI-Based Threat Detection in Cloud Security
  • Performance Optimization Techniques for Snowflake on AWS
  • The Modern Data Stack Is Overrated — Here’s What Works
  • Scalable System Design: Core Concepts for Building Reliable Software
  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.0K 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, DZone MVB. 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!