MessageSource is the Spring interface that resolves text messages by code and Locale. It lets you keep user-facing text in properties files, such as messages_en.properties and messages_fr.properties, and look it up at runtime so the same code can serve users in different languages. Every ApplicationContext is a MessageSource, so you rarely create one by hand.
It also supports parameters ({0}, {1}), default messages and a fallback chain of locales. Spring Boot configures one for you automatically when a messages.properties file is on the classpath.
org.springframework.context.MessageSource has three overloaded getMessage methods:
| Method | Behaviour |
|---|---|
getMessage(String code, Object[] args, String defaultMessage, Locale locale) | Returns the default message if the code is not found |
getMessage(String code, Object[] args, Locale locale) | Throws NoSuchMessageException if the code is not found |
getMessage(MessageSourceResolvable resolvable, Locale locale) | Resolves a code list plus arguments and default carried by one object |
The MessageSourceResolvable variant is what Spring validation uses: a FieldError implements it, so error messages for a field can be resolved from a list of candidate codes, from most specific to least specific.
ApplicationContext extends MessageSource, and delegates to a bean named messageSource if you define one. If no such bean exists, the context uses an empty internal source that only delegates to its parent.
Put one file per language under src/main/resources:
Spring resolves a code by trying the exact locale (fr_CA), then the language (fr), then the system default locale if fallbackToSystemLocale is true, then the base file. Parameters use java.text.MessageFormat syntax, so a literal single quote must be written as two ('').
Properties files are read as UTF-8 when you set the encoding explicitly. Java's ResourceBundle has read properties files as UTF-8 since Java 9, but you should still set setDefaultEncoding("UTF-8") (or the Boot property below) so Spring's own loader behaves the same.
Boot auto-configures MessageSourceAutoConfiguration when messages.properties exists. Tune it in application.properties:
The bean must be named messageSource, otherwise the ApplicationContext does not pick it up.
| Class | Backed by | Notes |
|---|---|---|
ResourceBundleMessageSource | java.util.ResourceBundle | Classpath only, simple, bundles are cached by the JDK |
ReloadableResourceBundleMessageSource | Spring Resource loading | Supports file: locations and refresh by cacheSeconds |
StaticMessageSource | In-memory map | Useful for tests |
Calling greet("Anna", Locale.FRENCH) returns:
In a Spring MVC controller you can take the Locale as a method argument, and Spring fills it in from the LocaleResolver (by default the Accept-Language header). Use LocaleContextHolder.getLocale() when you are deeper in the call stack.
Bean Validation messages can come from the same files if you wire the MessageSource into the validator:
A constraint such as @NotBlank(message = "{user.name.required}") then looks up user.name.required in your bundle. In Thymeleaf, #{greeting(${user.name})} resolves through the same source.
To pick the locale per request, register a LocaleResolver (for example AcceptHeaderLocaleResolver or CookieLocaleResolver) and, if users can switch languages, a LocaleChangeInterceptor.
No. If messages.properties is on the classpath, Boot registers one. Define your own bean named messageSource only when you need behaviour Boot's properties cannot express.
The first uses java.util.ResourceBundle and loads from the classpath. The second uses Spring's resource loading, so it can read from the file system and reload files without a restart.
The code is missing in every bundle for the resolved locale chain, and you called a variant without a default message. Add the key to the base file or pass a default message.
The properties file is read with the wrong encoding. Set spring.messages.encoding=UTF-8 or setDefaultEncoding("UTF-8") and save the file as UTF-8.
MessageSource resolves locale-specific text by code, with arguments and fallbacks. The ApplicationContext already implements it, Spring Boot configures it from spring.messages.*, and validation and view layers reuse the same bundles. Keep a base messages.properties, add one file per language, and set UTF-8 explicitly.