Home  >  Spring Core

ResourceBundleMessageSource Spring Example

By Arvind Rai, August 15, 2013 | Modified on June 19, 2016
On this page we will provide ResourceBundleMessageSource spring example. It is the implementation of MessageSource. ResourceBundleMessageSource accesses the resource bundle for the given base names. It uses java.util.ResourceBundle for its implementation. MessageSource provides the caching for ResourceBundle and MessageFormat for each message. The base names follow the ResourceBundle conventions. We need to specify fully qualified classpath location. Here on this page we will provide ResourceBundleMessageSource example using annotation as well as XML configuration.

ResourceBundleMessageSource using Annotation

To use ResourceBundleMessageSource using annotation, we will create a bean for it in JavaConfig. We need to provide message source base names. Find the complete example.
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 
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'SpringDemo'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter:1.3.3.RELEASE'
}  
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 

ResourceBundleMessageSource using 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 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us