简体   繁体   中英

Proper way of passing constant messages in Spring boot application (messages.properties or Constant class)

I have just noticed that the code I am working on has a messages.properties file where all the messages are written that should be passed.

My question is, why was this used instead of a Constants file where all the messages can be stored as constant Strings? I mean when I write them as Strings I get the suggestions in my IDE for them. Any specific reason why messages.properties approach was chosen?

The messages.properties files are meant more to internationalization:

By default, a Spring Boot application will look for message files containing internationalization keys and values in the src/main/resources folder: The file for the default locale will have the name messages.properties, and files for each locale will be named messages_XX.properties, where XX is the locale code.

The keys for the values that will be localized have to be the same in every file, with values appropriate to the language they correspond to.

If a key does not exist in a certain requested locale, then the application will fall back to the default locale value.

It's possible to achieve internationalization with a Constants file but not that easily.

Property files are not meant to be a substitute or an alternative to constants. They are used to store configurable values such as an ip, languages, etc. Loading constants from a file defeats the purpose of a constant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM