How to access properties from messages.properties file in spring controller using annotation.
Please provide an example.
The @Value annotation is a pre-defined annotation used to read values from any property files under the project's classpath.
Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation. Spring @Value annotation also supports SpEL.
Step 1 − After creating an executable JAR file, run it by using the command java –jar <JARFILE>. Step 2 − Use the command given in the screenshot given below to change the port number for Spring Boot application by using command line properties.
I used MessageSource :
@Autowired
private MessageSource messageSource;
...
private EventByDate createDefaultEventByDate(String date, Long barId, String locale) {
Event defaultEvent = new Event();
Locale localeValue = new Locale(locale);
defaultEvent.setTitle(messageSource.getMessage("default.event.title", null, "DefaultTitle", localeValue));
defaultEvent.setText(messageSource.getMessage("default.event.text", null, "DefaultText", localeValue));
...
}
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