如何在 Spring YAML 配置中转义 SpEL 美元符号? [英] How to escape SpEL dollar signs in Spring YAML configuration?

查看:35
本文介绍了如何在 Spring YAML 配置中转义 SpEL 美元符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Spring YAML 配置文件中,我需要有一个像

In a Spring YAML configuration file, I need to have a parameter like

csv:
  file:
    pattern: /some/path/${app-name}.csv

其中 ${app-name} 在运行时被 Java 代码动态替换,我不希望 Spring 在启动时替换它.

where the ${app-name} is dynamically replaced in run time by the Java code, and I do not want Spring to replace it at the startup.

为了实现这一点,我需要对 $ 字符进行转义,以便 Spring 不会将其解释为 SpEL.

To achieve this, I need to escape the $ character so Spring does not interpret it as SpEL.

以下答案在 YAML 中不起作用:

#{'$'} Spring 属性文件中的转义属性引用

无法读取包含美元符号 (${var}) 的属性

我尝试了所有的组合,比如

I tried all the combinations, like

pattern: /some/path/\${app-name}.csv
pattern: "/some/path/\${app-name}.csv"
pattern: /some/path/#{'$'}{app-name}.csv
pattern: "/some/path/#{'$'}{app-name}.csv"

并且它们都没有产生包含请求字符串的变量,带有美元符号但没有转义字符.

and none of them produces the variable containing the requested string, with the dollar sign but without the escape characters.

请注意是YAML配置.在 YAML 文件中,# 是行注释字符,从这个字符开始的所有内容都将被忽略.如果我使用 \#\ 然后被传递给字符串.

Please notice that it is YAML configuration. In YAML files, # is the line comment character, everything from this character on is ignored. And if I use \#, the \ is then passed to the string.

ADDED:有一个 Spring 项目公开问题 9628自 2008 年 6 月 25 日起开放:

ADDED: There has been an Spring project open issue 9628 open since 25.06.2008:

目前没有办法注入一个 ${...} 表达式,它不会被 PropertyPlaceholderConfigurer 选择.理想情况下,我们应该能够注入一个包含 ${...} 的字符串,以便稍后在其目标 bean 中使用,而无需 PropertyPlaceholderConfigurer 参与.

There is presently no way to inject a ${...} expression that won't be picked up by PropertyPlaceholderConfigurer. Ideally we should be able to inject a string that contains ${...} for later use in its target bean without involvement from PropertyPlaceholderConfigurer.

推荐答案

我遇到了同样的问题,我刚刚找到了 dumb 聪明的解决方案定义一个名为 DollarSign 或简称为 ds 的属性.

I had the same problem, i just found dumb clever solution define a property named dollarSign or ds for short.

ds: "$"

然后像这样使用它,${ds} 将在运行时替换为 $.

then use it like so, ${ds} will be replace by $ at runtime.

csv:
  file:
    pattern: /some/path/${ds}{app-name}.csv

当它起作用时有点有趣.

it was kind of funny when it worked.

这篇关于如何在 Spring YAML 配置中转义 SpEL 美元符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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