135

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?
sjngm
  • 11,411
  • 13
  • 72
  • 105

4 Answers4

147

${...} 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.

nlucaroni
  • 45,818
  • 5
  • 58
  • 85
skaffman
  • 381,978
  • 94
  • 789
  • 754
7

${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.

kryger
  • 11,746
  • 8
  • 41
  • 60
XvrCJ
  • 175
  • 1
  • 3
  • 7
    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 – gregfqt Mar 18 '15 at 14:00
  • 3
    The last statement on the JSP using SpEL is purely wrong. – Michal M Apr 28 '15 at 13:47
  • 2
    To clarify even further: JSP as of version 2.1. supports Unified EL http://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." – Fritz Duchardt Jun 17 '15 at 08:23
-1

Try reading this article, which suggests

"If the hash is used, your code is recomputed every time that element is included in a partial refresh (i.e. each time it is rendered). If you use a dollar, your code is only computed when the page is initially loaded. But this has been extended beyond just EL, to SSJS too. After the hash or dollar, the curly braces denote the start and end of your language. This will be important when we come to combining languages later."

chrisp7575
  • 667
  • 4
  • 3
  • 13
    None of has has anything to do with Spring EL, it's talking about JSP EL. – skaffman Mar 16 '11 at 08:36
  • @skaffman: even though you are right, of course, it's an interesting info (at least for me...). – sjngm Mar 16 '11 at 08:44
  • [Another answer](http://stackoverflow.com/a/20133659/39396) says that "there is no JSP EL, JSP uses SpEL". Is that correct? – Carl G Oct 29 '14 at 20:34
  • 1
    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 Mar 18 '15 at 13:59
-4

Expression Language Specification • Final Release - May 8, 2006

Page 2:

An eval-expression is formed by using the constructs ${expr} or #{expr}. Both constructs are parsed and evaluated in exactly the same way by the EL, even though they might carry different meanings in the technology that is using the EL.

Mellon
  • 85
  • 1
  • 6
  • 2
    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. – sjngm Feb 17 '13 at 17:12