application.yml中的反斜杠与占位符
给定的是一个Spring Boot application.yml 文件。
host: example.com
unc-path: \\${host}\share
如果打印出 unc-path,输出将是 \${host}\share。
我希望占位符 ${host} 能被解析。输出应当是 \\example.com\share。
解决方案
所述问题自Spring Framework 6.2版本起就存在。该版本默认开始支持使用字符 \ 对类似 ${host} 的属性占位符进行转义 [1]。
尚无法对转义字符本身进行转义 [2]。
解决方案1:
host: example.com
prefix: \\
unc-path: ${prefix}${host}\share
解决方案2:
保持YAML文件不变。
host: example.com
unc-path: \\${host}\share
添加以下属性(自6.2.7版起可用),它允许修改转义字符 [1]。如果设置为空值(如下所示),将恢复到Spring 6.2之前的行为,即不再进行转义。
spring.placeholder.escapeCharacter.default=
该属性必须放在位于 \src\main\resources 的 spring.properties 文件中。
[1] https://github.com/spring-projects/spring-framework/issues/34865
[2] https://github.com/spring-projects/spring-framework/issues/34315
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。