如何获得包含占位符的属性的原始值? [英] How to get raw value of property containing placeholders?

查看:218
本文介绍了如何获得包含占位符的属性的原始值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的.properties文件中定义以下属性:

I am trying to define the following property in one of my .properties files:

personExpression=${person.surname}

然后由配置类读取:

@Configuration
public class TemplateConfig {
    @Autowired
    private Environment environment;

    public String getPersonExpression() {
        return environment.getProperty("personExpression");
    }
}

但是这给出了例外:

java.lang.IllegalArgumentException: Could not resolve placeholder 'person.surname' in string value "${person.surname}"

有没有办法获得 getPersonExpression()返回字符串文字 $ {person.surname} 而不试图解决它?

Is there a way to do get getPersonExpression() to return the string literal ${person.surname} without attempting to resolve it?

推荐答案

要实现这一点需要一些非常不直观的语法。

To get this to work takes some pretty unintuitive syntax.

你必须将你的表达式分成两部分并将整个事物包装在父SpEL表达式中才能加入他们。

You essentially have to split your expression into two parts and wrap the whole thing in a parent SpEL expression to join them.

如果你将你的财产价值改为以下它应该有效:

If you change your property value to the following it should work:

personExpression=#{'$' + '{person.surname}'}

这是因为你从 {person.surname} 中分割 $ 字符,所以SpEL赢了牛逼尝试将其评估为表达式,因为就它而言,你只是连接两个字符串在一起。

This works because you're splitting up the $ character from the {person.surname} so SpEL won't try to evaluate it as an expression, because as far as it's concerned, you're just concatenating two strings together.

这篇关于如何获得包含占位符的属性的原始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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