Spring 引导更改值属性文件

Spring Boot change value properties file

您好,我正在使用 spring 启动。我想动态替换属性文件中变量的内容。

这是我的文件:message.properties

message=Welcome ${bean.name}  to my website

我想知道是否有任何方法可以更改我的变量值。 谢谢

如果是 messages.properties 文件,您不必动态更改其内容。相反,您可以使用消息变量。看看这个例子:

messages.properties:

message=Welcome {0} to my website

如果您使用 MessageSource bean 处理该消息,您可以通过以下方式获取此消息:

messageSource.getMessage("message", new Object[] { "Test" }, LocaleContextHolder.getLocale())

在这种情况下返回的字符串是:

Welcome Test to my website

当然你需要注入MessageSource到class(controller,service)才能使用这个示例代码:

@Autowired
MessageSource messageSource