如何在spring中的属性文件中设置占位符值
问题描述:
下面是 application.properties 文件
Below is application.properties file
app.not.found=app with {0} name can not be found.
如何在 spring 中用某个值替换 {0}?
How to replace {0} with some value in spring?
我正在使用以下代码读取属性文件值.
I am using below code to read properties file values.
env.getProperty("app.not.found")
但不知道如何设置占位符值.
but not getting how to set placeholder values.
答
使用 MessageFormat.format(String pattern, Object ... arguments)
.它在第二个参数中接受一个数组,它将依次替换 0, 1 , 2 ....
Use MessageFormat.format(String pattern, Object ... arguments)
. It accepts an array in second parameter, which will replace 0, 1 , 2 ... sequentially.
MessageFormat.format(env.getProperty("app.not.found"), obj)
obj 将替换字符串中的 {0}.
obj will replace {0} in your string.