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

sjngm picture sjngm · Mar 16, 2011 · Viewed 46.2k times · Source

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?

Answer

skaffman picture skaffman · Mar 16, 2011

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