Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)
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.
There is no JSP EL, JSP uses SpEL. SpEL fits to technology that is using it.
Comments
-
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 about 13 yearsNone of has has anything to do with Spring EL, it's talking about JSP EL.
-
sjngm about 13 years@skaffman: even though you are right, of course, it's an interesting info (at least for me...).
-
sjngm about 11 yearsWhich 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 over 9 yearsAnother answer says that "there is no JSP EL, JSP uses SpEL". Is that correct?
-
gregfqt about 9 yearsThere 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 about 9 yearsYou'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 almost 9 yearsThe last statement on the JSP using SpEL is purely wrong.
-
Fritz Duchardt almost 9 yearsTo 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 about 8 yearsIn order to successfully access property values using the SpEL syntax, use the format
"#{'${property}'}"
-
Steve Chambers about 6 yearsRe: comment above - also see this answer