Could not read properties if it contains dollar symbol (${var})

折月煮酒 提交于 2019-12-12 20:07:11

问题


I have property file with following content:

INVALID_ARGUMENT=Field ${fieldName} is invalid or missing.

I read this using spring configuration:

@PropertySource("error_messages_en.properties")
@Configuration
public static class ErrorMessagesEn {
     @Value("${INVALID_ARGUMENT}")
    private String invalidArgument;
}

But after application start I see:

Could not resolve placeholder 'fieldName' in value "Ïîëå ${fieldName} íåêîððåêòíî çàïîëíåíî èëè ïðîïóùåíî."

I need ${fieldName} because I want to use repacer: https://stackoverflow.com/a/3655963/2674303

How to avoid this error?


回答1:


You should escape ${fieldName} as it is not defined as a Spring property.

You could have a look at this question: Escape property reference in spring property file and the associated answer.

You should write this instead:

INVALID_ARGUMENT=Field #{'$'}{fieldName} is invalid or missing.


来源:https://stackoverflow.com/questions/43493857/could-not-read-properties-if-it-contains-dollar-symbol-var

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!