DEV Community

Cover image for ๐ŸŒฟ Spring Expression Language (SpEL): Dynamic Power at Your Fingertips
Arkadipta kundu
Arkadipta kundu

Posted on

๐ŸŒฟ Spring Expression Language (SpEL): Dynamic Power at Your Fingertips

In the last week, I went down a new rabbit hole in my Spring journey โ€” and this time, it was all about SpEL, aka Spring Expression Language.

At first, it felt like "Wait, what even is this sorcery with #{} and all these expressions?" but trust me, once I got the hang of it, I realized how powerful and flexible it is โ€” especially when it comes to injecting values dynamically into your beans.

So, hereโ€™s what I learned, written from one student to another. If you're also trying to get comfy with Springโ€™s core features, this post is for you ๐Ÿ‘‡


๐Ÿ“š Resources I Used


๐Ÿง  What is SpEL?

SpEL (Spring Expression Language) is like giving your annotations a brain โ€” it lets you write logic inside annotations using #{} syntax.

Itโ€™s used mainly for:

  • Injecting dynamic values into beans
  • Evaluating conditions
  • Calling methods or accessing variables
  • Creating objects on the fly

The idea is to add more flexibility and dynamic behavior to your Spring application, directly from annotations or XML.


๐Ÿ›  Basic SpEL Syntax

The syntax is pretty straightforward. You wrap expressions like this:

@Value("#{expression}")
private String value;
Enter fullscreen mode Exit fullscreen mode

Spring will evaluate the expression at runtime and inject the result.


๐Ÿ”ธ Injecting Values Dynamically

You can perform simple calculations, call methods, or reference beans.

@Value("#{20 + 30}")
private int total; // Will inject 50

@Value("#{T(java.lang.Math).random() * 100}")
private double randomValue; // Injects a random number
Enter fullscreen mode Exit fullscreen mode

You can also inject properties dynamically:

@Value("#{systemProperties['user.name']}")
private String userName;
Enter fullscreen mode Exit fullscreen mode

๐Ÿงฎ Calling Static Methods

You can call static methods using the T() operator.

@Value("#{T(java.lang.Math).sqrt(144)}")
private double squareRoot; // 12.0
Enter fullscreen mode Exit fullscreen mode

๐Ÿ›Ž Accessing Bean Properties

You can also access values from other beans:

@Component
public class Employee {
    private int salary = 50000;

    public int getSalary() {
        return salary;
    }
}

@Component
public class SalaryCalculator {
    @Value("#{employee.salary + 10000}")
    private int revisedSalary; // 60000
}
Enter fullscreen mode Exit fullscreen mode

โœ… Boolean Expressions in SpEL

You can use SpEL for logical conditions too:

@Value("#{2 > 1}") // true
private boolean isGreater;

@Value("#{employee.salary > 40000}")
private boolean isEligible;
Enter fullscreen mode Exit fullscreen mode

Itโ€™s also used in annotations like @Conditional or custom conditions when you want fine-grained control.


๐ŸŽฏ Object Creation with SpEL

Yep, you can create objects inside the annotation too ๐Ÿ˜ฎ

@Value("#{new java.util.Date()}")
private Date currentDate;
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”„ SpEL vs Traditional @Value

Before SpEL, we used @Value("some value") just to hardcode strings or values.

But with SpEL, it becomes a lot more dynamic and powerful. Youโ€™re not just injecting static values โ€” you're injecting logic.


๐Ÿ’ก Where Youโ€™ll See SpEL in Real Life

  • Dynamically calculating tax, discounts, etc.
  • Injecting user-specific or system environment values
  • Enabling/disabling features conditionally
  • Loading properties from config files and manipulating them

I feel like this becomes really useful once your app gets a bit more real-world and config-heavy.


๐Ÿงช My Code Experiments

You can find all the code examples I tried related to SpEL here:

๐Ÿ‘‰ GitHub - Spring Expression Language Experiments

These include:

  • Static method calls
  • Object creation
  • Expression evaluations
  • Realistic salary-based examples ๐Ÿ˜„

๐Ÿš€ Whatโ€™s Next?

Iโ€™m continuing my Spring journey. Next up:

  • More on annotations like @ComponentScan, @Conditional
  • Dive into Spring Boot real-world use cases
  • Slowly transition into building mini projects (finally ๐Ÿ˜…)

๐Ÿฅœ In a nutshell (TL;DR)

  • SpEL (Spring Expression Language) allows dynamic value injection using #{} syntax.
  • You can evaluate expressions, do calculations, access properties, or even create objects.
  • You can call static methods (T(className)), access beans, and use booleans/conditions.
  • Super useful when you want beans to be flexible without writing extra logic.
  • Itโ€™s like giving superpowers to your annotations ๐Ÿ˜Ž

Thatโ€™s it from me for this week!

If you're learning Spring too, feel free to connect or check out my other articles.

Weโ€™ll get there โ€” one annotation at a time! ๐Ÿ’ช

Top comments (0)