带有@Value 的 Spring 表达式语言 (SpEL):美元 vs. 哈希($ vs. #) [英] Spring Expression Language (SpEL) with @Value: dollar vs. hash ($ vs. #)

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

问题描述

#{...} 相比,我对何时使用 ${...} 有点困惑.Spring 的文档只使用了 #{...},但是有很多使用 ${...} 的例子.此外,当我开始使用 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;

    :
}

和一些属性文件:

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

我的问题是:

  • 有何不同或相同之处?
  • 是否已弃用一个版本,所以我应该使用另一个版本?

推荐答案

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

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

#{...}SpEL 语法,它的功能和复杂得多.它还可以处理属性占位符等等.

#{...} 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):美元 vs. 哈希($ vs. #)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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