使用@Value的Spring表达式语言(Spel):dollar vs. hash($ vs.#) [英] Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)

查看:233
本文介绍了使用@Value的Spring表达式语言(Spel):dollar vs. hash($ vs.#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 $ {...} #{...} 。 Spring的文档仅使用#{...} ,但有很多示例使用 $ {...} 。此外,当我开始使用SpEL时,我被告知要使用 $ {...} ,它的工作正常。



对于那些困惑的人,我使用它的一个例子将是

  @Component 
public class ProxyConfiguration {

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


}

和一些属性文件: p>

  proxy.host = myproxy.host 
proxy.port = 8000
/ pre>

我的问题是:




  • 有什么区别?相同?

  • 是一个不推荐的版本,所以我应该使用另一个版本?


解决方案

$ {...} 是属性占位符语法。它只能用于取消引用属性。



#{...} Spel语法,它的功能和复杂性更强。它也可以处理属性占位符,而且还有很多。



两者都是有效的,都不会被弃用。


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?

解决方案

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

这篇关于使用@Value的Spring表达式语言(Spel):dollar vs. hash($ vs.#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆