Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)

Cover Image for Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)

Are you confused about when to use ${...} compared to #{...} in Spring's Expression Language (SpEL)? You're not alone! Spring's documentation mainly uses #{...}, but there are plenty of examples that use ${...}. To make matters even more confusing, some people may have suggested using ${...} when you first started with SpEL and it seemed to work fine. So what's the difference, and which one should you use?

Let's dive into this topic and clarify things once and for all. 🤔

What's the Difference?

Both ${...} and #{...} are used for expressing values in SpEL, but they have slightly different behaviors and use cases.

1. Dollar Sign ($)

The dollar sign $ is used when you want to access a property value from the Spring environment or system properties.

In the provided example:

@Component
public class ProxyConfiguration {

    @Value("${proxy.host}")
    private String host;
    @Value("${proxy.port}")
    private String port;

    :
}

The ${proxy.host} and ${proxy.port} expressions retrieve the values of proxy.host and proxy.port properties from a property file (e.g., application.properties, application.yml, etc.).

The dollar sign form (${...}) is widely used in traditional Spring applications, especially for retrieving externalized configuration values.

2. Hash (#)

On the other hand, the hash symbol # is used when you want to evaluate an expression or invoke a bean method. It is more powerful and flexible than the dollar sign form.

For example:

@Component
public class ExampleBean {

    @Value("#{someBean.doSomething()}")
    private String result;

    :
}

In this case, #someBean.doSomething() will invoke the doSomething() method on someBean and assign the result to the result field.

The hash symbol form (#{...}) is especially useful when you need complex expressions or want to perform operations using Spring beans.

Are They the Same?

No, they are not the same. The dollar sign form (${...}) is used for accessing property values, while the hash symbol form (#{...}) is used for evaluating expressions or invoking bean methods.

Which One Should You Use?

Now that the differences are clear, which one should you use?

  • If you only need to access a simple property value, such as retrieving configuration settings, use the dollar sign form (${...}). This is the conventional and recommended way.

  • If you need to evaluate an expression or invoke a method, use the hash symbol form (#{...}). It provides more flexibility and power.

It's important to note that using the hash symbol form (#{...}) allows you to access the full power of SpEL, while the dollar sign form (${...}) is more limited in its capabilities.

Conclusion

In summary, the dollar sign ($) and hash symbol (#) in SpEL have different meanings and use cases. The dollar sign ($) is used for accessing property values, while the hash symbol (#) is used for evaluating expressions or invoking bean methods.

So, next time you find yourself confused about which expression to use, remember these guidelines:

  • Use ${...} to access property values.

  • Use #{...} to evaluate expressions or invoke bean methods.

Keep these tips in mind, and you'll be confidently using SpEL expressions in your Spring applications! 💪

If you have any further questions or want to share your experience with SpEL, leave a comment below. Happy coding! 😄👍


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello