Home  >  Spring

Spring ResourceBundleMessageSource Example

By Arvind Rai, March 30, 2022
On this page we will learn ResourceBundleMessageSource in Spring.
1. The ResourceBundleMessageSource is the implementation of MessageSource interface which is used for resolving messages.
2. This message source caches both the accessed ResourceBundle instances and the generated MessageFormats for each message.
3. The basenames of this message source should follow ResourceBundle conventions. The basenames should be a fully qualified classpath location. If it does not contain package qualifier, the basenames are resolved from classpath root.

Now find the example where we will read a property file using ResourceBundleMessageSource.



With @Configuration

To use ResourceBundleMessageSource in annotation based application, we will create a bean for it in @Configuration class.
AppConf.java
package com.concretepage;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
@Configuration
public class AppConf {
    @Bean
    public MessageSource messageSource() {
    	ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasenames("i18/users", "i18/errormsg");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
} 
i18/errormsg_de.properties
errormsg.name= Enter name
errormsg.pwd= Enter password 
i18/users_de.properties
user.admin= Jay
user.superadmin=Surya 
SpringDemo.java
package com.concretepage;
import java.sql.SQLException;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.concretepage.AppConf;
public class SpringDemo {
	public static void main(String[] args) throws SQLException {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.register(AppConf.class);
		ctx.refresh();
		MessageSource resources = ctx.getBean(MessageSource.class);
	   	String admin = resources.getMessage("user.admin", null, "Default", new Locale("de"));
	   	System.out.println(admin);
	   	String superadmin = resources.getMessage("user.superadmin", null, "Default", new Locale("de"));
	   	System.out.println(superadmin);	   	
    	        String name = resources.getMessage("errormsg.name", null, "Default", new Locale("de"));
	   	System.out.println(name);
    	        String pwd = resources.getMessage("errormsg.pwd", null, "Default", new Locale("de"));
	   	System.out.println(pwd);	   	
		ctx.registerShutdownHook();
	}
} 
Output
Jay
Surya
Enter name
Enter password 

With XML Configuration

Find the ResourceBundleMessageSource example using XML configuration.
spring-app.xml
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">

  <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
	  <property name="basenames">
	    <list>
	      <value>i18/users</value>
	      <value>i18/errormsg</value>
	    </list>
	  </property>
  </bean>
</beans> 
SpringDemo.java
package com.concretepage;
import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringDemo {
    public static void main(String... args) {
    	        MessageSource resources = new ClassPathXmlApplicationContext("spring-app.xml");
 	   	String admin = resources.getMessage("user.admin", null, "Default", new Locale("de"));
 	   	System.out.println(admin);
 	   	String superadmin = resources.getMessage("user.superadmin", null, "Default", new Locale("de"));
 	   	System.out.println(superadmin);	   	
         	String name = resources.getMessage("errormsg.name", null, "Default", new Locale("de"));
 	   	System.out.println(name);
     	        String pwd = resources.getMessage("errormsg.pwd", null, "Default", new Locale("de"));
 	   	System.out.println(pwd);	   	
    } 
} 
Output
Jay
Surya
Enter name
Enter password 

Reference

Spring ResourceBundleMessageSource

Download Source Code

POSTED BY
ARVIND RAI
ARVIND RAI










©2022 concretepage.com | Privacy Policy | Contact Us