I wanted to substitute the placeholder dynamically in properties in a java application. Like
WelcomeMessage=Welcome Mr. {firstName} {lastName} !!!
These firstName and LastName variable needs to be substituted dynamically. Should we use velocity template engine for the same? Or are there any other opensource frameworks for the same?
Thanks, Manish
Spring Boot - Using ${} placeholders in Property Files We just need to use ${someProp} in property file and start the application having 'someProp' in system properties or as main class (or jar) argument '--someProp=theValue'. This feature allows us to use 'short' command line arguments.
You can use the MessageFormat class of Java SE. It allows you to do exactly what you ask for.
In your case the below code snippet must do the trick, assuming props contains all the properties loaded from your file.
MessageFormat.format((String) props.get("WelcomeMessage"), "First", "Last");
Note that your properties files should have index of parameters instead of named parameters as below.
WelcomeMessage=Welcome Mr. {0} {1} !!!
Velocity is rather old and unpleasant, in my opinion, there are nicer ways to do this:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With