Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access properties from messages.properties file in spring controller using annotation

How to access properties from messages.properties file in spring controller using annotation.

Please provide an example.

like image 899
vikasgupta Avatar asked Jan 10 '13 06:01

vikasgupta


People also ask

Which annotation is used to access custom properties file present in different location?

The @Value annotation is a pre-defined annotation used to read values from any property files under the project's classpath.

What is @value annotation in spring?

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.

How do you access properties in spring boot?

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.


1 Answers

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));
    ...
}
like image 173
Labe Avatar answered Sep 17 '22 12:09

Labe