我想在我的.properties文件之一定义以下属性:如何获取包含占位符的财产的原始价值?
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}
而不试图解决它?
是否有一些额外的配置,需要添加到获得春评价#{..}内的表达式? 我尝试将属性更改为'#{'$'+'{person.surname}'}'但'getPersonExpression()'刚刚返回'#{'$'+'{person.surname}'}' – IgorK
使用'PropertyPlaceholderConfigurer' bean?这应该读取您的属性文件,解析值,然后使其可用。如果你不是,那么属性文件将不会使用SpEL进行分析。 – JamesENL
是的,这是问题。我没有注册PropertyPlaceholderConfigurer bean,因为没有其他正在评估的属性。加入\t'@Bean \t public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){ \t \t return new PropertySourcesPlaceholderConfigurer(); \t}'做了诡计。此外,我搞砸了格式化了一下,并将属性更改为'personExpression =#{'$'} {person.surname}'这仍然有效,并且更容易阅读 – IgorK