MessageSource is the Spring interface for resolving text messages by key, locale and optional arguments. It is the foundation of internationalisation (i18n) in Spring: you keep user-facing text in per-language resource bundles and ask MessageSource for the right one at runtime.
Every Spring ApplicationContext is itself a MessageSource. In Spring Boot, a MessageSource bean is auto-configured when a messages.properties file exists on the classpath, and Spring Validation, Thymeleaf and Spring MVC all use it to localise labels and error messages.
You pass a message code (the key), optional arguments for placeholders such as {0}, and the Locale. If the key is missing, the second overload throws NoSuchMessageException, while the first returns the supplied default.
Create one properties file per language in src/main/resources:
Spring Boot reads the base name messages by default. Configure it in application.properties when you use other names or want other behaviour:
Properties files are read as UTF-8 by default in Boot, so accented and non-Latin text works without Unicode escapes. Setting fallback-to-system-locale=false makes an unknown locale fall back to messages.properties rather than the server's own language, which is usually what you want.
LocaleContextHolder.getLocale() returns the locale of the current request. Spring MVC sets it from the Accept-Language header through the default AcceptHeaderLocaleResolver. To let users choose a language, register a LocaleResolver bean such as CookieLocaleResolver and a LocaleChangeInterceptor that reads a lang parameter.
For a request with Accept-Language: fr, greet("Asha") returns:
| Class | Behaviour |
|---|---|
ResourceBundleMessageSource | Uses java.util.ResourceBundle, loads from the classpath, simple and fast |
ReloadableResourceBundleMessageSource | Uses Spring resources, supports file: locations and reloading with setCacheSeconds |
StaticMessageSource | Programmatic messages, useful in tests |
Spring Boot configures a ResourceBundleMessageSource for you. Define your own bean named messageSource if you need reloadable bundles outside the jar:
The bean name must be exactly messageSource, otherwise the context does not use it.
Bean Validation messages can come from the same bundles. Reference a key in braces in the annotation and make the validator use Spring's source:
Spring Boot wires MessageSource into the validator, so the key resolves for the current locale. Thymeleaf templates read messages with #{greeting(${name})}, and controllers can also inject MessageSource or use MessageSourceAccessor.
messages.properties, with locale variants such as messages_de.properties. Change it with spring.messages.basename.
By default Boot falls back to the JVM's system locale before the default bundle. Set spring.messages.fallback-to-system-locale=false and check that the request locale is being resolved.
Use {0}, {1} placeholders in the property value and pass an Object[] to getMessage. The syntax follows java.text.MessageFormat, so apostrophes must be doubled.
Yes, with ReloadableResourceBundleMessageSource and a cache time, or spring.messages.cache-duration in Boot.
MessageSource decouples user-facing text from code. Keep messages in locale-specific bundles, resolve them with the current Locale, configure encoding and fallback in Spring Boot, and reuse the same bundles for validation and templates.