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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Top 10 Pillars of Zero Trust Networks
  • Clear Details on Java Collection ‘Clear()’ API
  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
  • How to Handle Secrets in Kubernetes
  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!

Swathi Prasad user avatar by
Swathi Prasad
·
Oct. 08, 19 · Presentation
Like (19)
Save
Tweet
Share
54.82K 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.

Trending

  • Top 10 Pillars of Zero Trust Networks
  • Clear Details on Java Collection ‘Clear()’ API
  • Cypress Tutorial: A Comprehensive Guide With Examples and Best Practices
  • How to Handle Secrets in Kubernetes

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: