spring boot messagesource example

Spring Boot MessageSource Example: Internationalization in Action

By Emily Jones
AD

Spring Boot, a popular Java-based framework, simplifies application development by providing features like auto-configuration and dependency injection. One of its powerful features is the ability to externalize messages, enabling applications to be more flexible and maintainable. This is achieved using the `MessageSource` interface, which provides methods to retrieve messages based on locales. Let's explore an example of using `MessageSource` in Spring Boot.

Before diving into the example, ensure you have the Spring Boot Starter parent in your project's dependencies. This starter includes the `MessageSourceAutoConfiguration` class, which automatically configures a `MessageSource` if certain conditions are met.

Configuring MessageSource in Spring Boot

Spring Boot automatically configures a `MessageSource` if it finds a `ResourceBundle` or a `Properties` file in the `src/main/resources` folder. These files can contain messages that are keyed by codes, allowing you to retrieve them based on the locale.

AD
#springboot #javadeveloper #backenddevelopment #softwarearchitecture #apis #microservices #systemdesign #cloudengineering #devops #programmingtips | Ignacio C.
#springboot #javadeveloper #backenddevelopment #softwarearchitecture #apis #microservices #systemdesign #cloudengineering #devops #programmingtips | Ignacio C.
Spring Boot Real-Time Project
Spring Boot Real-Time Project
Spring Boot Security-Scheduling-Transactions Annotations With Examples
Spring Boot Security-Scheduling-Transactions Annotations With Examples

For example, create a `messages.properties` file in your `src/main/resources` folder with the following content:

hello=Hello, {0}!

In this file, `hello` is the message code, and `Hello, {0}!` is the message itself. The `{0}` placeholder will be replaced with an argument when retrieving the message.

Spring Boot: Soft Delete functionality with Hibernate
Spring Boot: Soft Delete functionality with Hibernate
Login and Signup with Java and Spring Boot
Login and Signup with Java and Spring Boot
Spring Boot cheatsheet - Masterspringboot
Spring Boot cheatsheet - Masterspringboot

Using MessageSource in Controllers

To use `MessageSource` in your controllers, autowire it as a dependency. Then, you can use the `getMessage` method to retrieve messages based on the provided code and arguments.

Here's an example of a Spring Boot controller using `MessageSource`:

AD
```java @Controller public class GreetingController { @Autowired private MessageSource messageSource; @GetMapping("/greet") public String greet(@RequestParam(name = "name", defaultValue = "World") String name, Locale locale) { String greeting = messageSource.getMessage("hello", new Object[]{name}, locale); return greeting; } } ```

In this example, the `greet` method retrieves the message with the code `hello` and replaces the `{0}` placeholder with the provided `name` argument. The `locale` parameter is used to determine the appropriate message based on the user's locale.

Spring Boot Security and JWT
Spring Boot Security and JWT
spring boot value annotation
spring boot value annotation
Top 10 Spring Boot Interview Questions
Top 10 Spring Boot Interview Questions
Spring Boot Project Ideas for Resume | Real-World Examples
Spring Boot Project Ideas for Resume | Real-World Examples
I Will Create Web App Using Angular Spring Boot
I Will Create Web App Using Angular Spring Boot
8 Best Spring Boot Courses for Java Developers in 2026
8 Best Spring Boot Courses for Java Developers in 2026

Using MessageSource in Services

You can also use `MessageSource` in your services to retrieve messages that can be used across multiple controllers or components. Here's an example:

```java @Service public class GreetingService { @Autowired private MessageSource messageSource; public String greet(String name, Locale locale) { return messageSource.getMessage("hello", new Object[]{name}, locale); } } ```

In this service, the `greet` method follows the same pattern as the controller example, allowing you to centralize message retrieval and reuse it across your application.

AD

Using MessageSource with Different Locales

Spring Boot automatically determines the user's locale based on the `Accept-Language` header in the HTTP request. To provide messages for different locales, create separate resource bundles for each locale.

Spring Framework Cheat Sheet for Beginners | Spring Boot Quick Guide for Java Developers
Spring Framework Cheat Sheet for Beginners | Spring Boot Quick Guide for Java Developers
Spring Boot RESTful CRUD Example with MySQL Database - Websparrow
Spring Boot RESTful CRUD Example with MySQL Database - Websparrow
Spring Boot DATA JPA CRUD Web App - Quick Hands On
Spring Boot DATA JPA CRUD Web App - Quick Hands On
Spring Boot + Redis Example in Java
Spring Boot + Redis Example in Java
Spring Boot Application Properties Example - Java Code Geeks
Spring Boot Application Properties Example - Java Code Geeks
Entry Point of a Springboot Application
Entry Point of a Springboot Application
Spring Boot Folder Structure: Best Practices and Guide
Spring Boot Folder Structure: Best Practices and Guide
the spring data boot logo is shown in front of a blue background
the spring data boot logo is shown in front of a blue background
Building a chat application with Spring Boot and WebSocket
Building a chat application with Spring Boot and WebSocket
Spring Boot 4 Projects Best Codes
Spring Boot 4 Projects Best Codes
How To Work With Apache Kafka In Spring Boot?
How To Work With Apache Kafka In Spring Boot?
a man wearing a white shirt and tie standing in front of a sign that says spring boot training
a man wearing a white shirt and tie standing in front of a sign that says spring boot training
Java + Spring Boot + Microservices Example - Step by Step Guide
Java + Spring Boot + Microservices Example - Step by Step Guide
Spring Boot Tutorial for Beginners 8 - Configuring Embedded Apache Derby in Spring Boot app
Spring Boot Tutorial for Beginners 8 - Configuring Embedded Apache Derby in Spring Boot app

For example, create a `messages_es.properties` file for Spanish messages:

hello=¡Hola, {0}!

Now, when a user requests the `/greet` endpoint with the `Accept-Language` header set to `es`, the controller will retrieve the Spanish message.

AD

Specifying the Default Locale

To specify a default locale for your application, add the following property to your `application.properties` file:

spring.messages.default-encoding=UTF-8 spring.messages.defaultlocale=en

In this example, the default locale is set to English (`en`). If a user's locale cannot be determined or does not have a corresponding resource bundle, the default locale will be used.

In conclusion, using `MessageSource` in Spring Boot allows you to externalize messages, making your applications more flexible and maintainable. By leveraging resource bundles and locales, you can easily provide messages for different languages and regions. Embrace this feature to create more robust and user-friendly applications.

AD