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;

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! 💪