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
- ๐ฅ YouTube Playlist (Easy Programming)
- ๐ Spring Official Docs - SpEL
- ๐ Udemy: Spring 5 with Spring Boot 2
- ๐ป GitHub - All my SpEL code
๐ง 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;
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
You can also inject properties dynamically:
@Value("#{systemProperties['user.name']}")
private String userName;
๐งฎ Calling Static Methods
You can call static methods using the T()
operator.
@Value("#{T(java.lang.Math).sqrt(144)}")
private double squareRoot; // 12.0
๐ 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
}
โ 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;
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;
๐ 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)