Spring Boot Change Value Properties File

Hi I am using spring boot. I would like to dynamically replace the contents of a variable in a properties file.

This is my file: message.properties

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

      

I would like to know if there is a way to change the value of my variable. Thanks to

+3


source to share


1 answer


When it comes to a file messages.properties

, you don't need to dynamically change its contents. You can use variable messages instead. Take a look at this example:

messages.properties:

message=Welcome {0} to my website

      

If you are handling this message with a MessageSource

bean, you can get this message with

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

      



The returned string in this case:

Welcome Test to my website

      

Of course, you need to inject MessageSource

into a class (controller, service) before you can use this example code:

@Autowired
MessageSource messageSource

      

+6


source







All Articles