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

52,745

Solution 1

${...} is the property placeholder syntax. It can only be used to dereference properties.

#{...} is SpEL syntax, which is far more capable and complex. It can also handle property placeholders, and a lot more besides.

Both are valid, and neither is deprecated.

Solution 2

${expr} --> Immediate Evaluation

#{expr} --> Deferred Evaluation

Immediate evaluation means that the expression is evaluated and the result returned as soon as the page is first rendered. Deferred evaluation means that the technology using the expression language can use its own machinery to evaluate the expression sometime later during the page’s lifecycle, whenever it is appropriate to do so.

Complete reference here

There is no JSP EL, JSP uses SpEL. SpEL fits to technology that is using it.

Share:
52,745
sjngm
Author by

sjngm

(my about me is currently blank)

Updated on March 24, 2020

Comments

  • sjngm
    sjngm about 4 years

    I'm a little confused concerning when to use ${...} compared to #{...}. Spring's documentation only uses #{...}, but there are plenty of examples that use ${...}. Furthermore, when I started with SpEL I was told to use ${...} and it works fine.

    For those who are confused, an example of how I use it would be

    @Component
    public class ProxyConfiguration {
    
        @Value("${proxy.host}")
        private String host;
        @Value("${proxy.port}")
        private String port;
    
        :
    }
    

    and some property file:

    proxy.host=myproxy.host
    proxy.port=8000
    

    My questions are:

    • what are the differences or is it the same?
    • is one version deprecated so I should use the other one?
  • skaffman
    skaffman about 13 years
    None of has has anything to do with Spring EL, it's talking about JSP EL.
  • sjngm
    sjngm about 13 years
    @skaffman: even though you are right, of course, it's an interesting info (at least for me...).
  • sjngm
    sjngm about 11 years
    Which part of my question is this supposed to answer? It's from 2006 and words like "parsed and evaluated in exactly the same way" and "might carry different meanings" is probably as vague is you can probably put it.
  • Carl G
    Carl G over 9 years
    Another answer says that "there is no JSP EL, JSP uses SpEL". Is that correct?
  • gregfqt
    gregfqt about 9 years
    There is an "Expression Language" (EL) in the Java EE spec which is meant to be used in JSPs and by JSF. It has nothing to do with Spring EL.
  • gregfqt
    gregfqt about 9 years
    You're talking about a Java EE web technology while the question was about spring config using Spring ELs. And then, JSP do not use SpEL, you don't even need Spring to run JSPs
  • Michal M
    Michal M almost 9 years
    The last statement on the JSP using SpEL is purely wrong.
  • Fritz Duchardt
    Fritz Duchardt almost 9 years
    To clarify even further: JSP as of version 2.1. supports Unified EL docs.oracle.com/javaee/5/tutorial/doc/bnahq.html which served as a template for SpEL. From the Spring docu: "The Spring Expression Language (SpEL for short) is a powerful expression language that supports querying and manipulating an object graph at runtime. The language syntax is similar to Unified EL but offers additional features, most notably method invocation and basic string templating functionality."
  • Brice Roncace
    Brice Roncace about 8 years
    In order to successfully access property values using the SpEL syntax, use the format "#{'${property}'}"
  • Steve Chambers
    Steve Chambers about 6 years
    Re: comment above - also see this answer