Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot change value properties file

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

This is my file: message.properties

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

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

like image 651
Diego Avatar asked Apr 22 '15 17:04

Diego


People also ask

How do I change spring boot properties?

To change properties in a file during runtime, we should place that file somewhere outside the jar. Then we tell Spring where it is with the command-line parameter –spring. config. location=file://{path to file}.

How use value from properties file in spring boot?

Another method to access values defined in Spring Boot is by autowiring the Environment object and calling the getProperty() method to access the value of a property file.

Can we change application properties in spring boot?

Spring boot provides command line configuration called spring.config.name using that we can change the name of application. properties. Here properties file name will be my-config.


1 Answers

If it comes to messages.properties file, you don't have to change dynamically its content. Instead you can use message variables. Take a look at this example:

messages.properties:

message=Welcome {0} to my website

If you process that message using MessageSource bean, you can get this message with:

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

The returned string in that case is:

Welcome Test to my website

Of course you need to inject MessageSource to the class (controller,service) before you can use this exemplary code:

@Autowired
MessageSource messageSource
like image 64
Szymon Stepniak Avatar answered Nov 15 '22 07:11

Szymon Stepniak