Using MessageSource in Spring
Messaging support and Internationalization is provided by Spring ApplicationContext and this is one of the advantages of using Application Context over BeanFactory. If we have different messages that need to be displayed in application UI and we would not want those messages to be inside the code, we keep those messages in properties file and then refer to these texts inside the properties file. Spring provides support for this. We can have a properties file which consolidates all the key and value pairs and then we specify the key in our application to refer to that text which is assigned to that key in the properties file. Let us see how we can use this support for Messages in Spring.
mymessages.properties
1 |
greeting=Hello! |
We have the message ‘Hello!’ saved inside a properties file and we have given it a key of ‘greeting’. We will use this key-value pair and print ‘Hello!’ message in our program.
The first thing we need to have in order to have Messaging Support enabled is to have a message source bean defined in the application.
spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>mymessages</value> </list> </property> </bean> <context:annotation-config/> <context:component-scan base-package="com.j2eereference.spring"></context:component-scan> </beans> |
The bean added to support the message service the bean with id=”messageSource” and the class ‘ResourceBundleMessageSource’ (give the fully qualified name as mentioned in spring.xml). This class is provided by Spring. It helps us pick messages from properties file. This bean needs some more configuration – we need to tell the bean where the properties file is. To do that, we have added a property tag with name ‘basenames’ and within which we define the list of properties file names(without .properties extension). We have only one properties file in our example. If there are multiple properties files, we can list out all the properties file over here. This is what The messageSource is going to look at when it has to pull up messages.
MessageSourceDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.j2eereference.spring; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MessageSourceDemo{ public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); System.out.println(context.getMessage("greeting",null,"Default Greeting", null)); } } |
Output:
Hello!
The ‘context’ object has a method called getMessage(). There are different signatures for this method. Lets look into the one that is used here. It takes four parameters –
First parameter – the name of the property that we need to retrieve from the properties file. ‘greeting’ in this example.
Second parameter – A string of object in case the message takes in some parameters. We will look at parameter messages later. Since we don’t have parameter in our message and it is a simple text, we will keep it null.
Third Parameter – The default message that it needs to return in case it does not find a message that we are looking for.’Default Greeting’ in our example. This message will be shown if a corresponding message for the ‘greeting’ key is not found in any of the properties files.
Fourth parameter – the locale for which we are going to get the message. We will keep it null for now and explain it later.
Injecting MessageSource into a bean:
Here lets understand how to inject the MessageSource into a bean with the below example
mymessages.properties
1 |
greeting=Hello Good morning ! |
spring.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="Greetings" class="com.j2eereference.spring.messageSource.Greetings" > <property name="messageSrc" ref="messageSource"/> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>mymessages</value> </list> </property> </bean> <context:annotation-config/> <context:component-scan base-package="com.j2eereference.spring"></context:component-scan> </beans> |
Greetings.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.j2eereference.spring.messageSource; import org.springframework.context.MessageSource; public class Greetings { MessageSource messageSrc; public MessageSource getMessageSrc() { return messageSrc; } public void setMessageSrc(MessageSource messageSrc) { this.messageSrc = messageSrc; } public void display() { System.out.println(this.messageSrc.getMessage("greeting", null,"Default Greeting", null)); } } |
MessageSourceDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package com.j2eereference.spring.messageSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MessageSourceDemo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); Greetings greetings= (Greetings)context.getBean("Greetings"); greetings.display(); } } |
Output:
1 |
Hello Good morning ! |
Using placeholder in MessageSource:
This is fine but how do we replace the sysout that prints the branch information, using message source? As mentioned earlier, the second parameter of the getMesssage() is a string object that in case the message takes in some parameters. Now lets add one more entry in the properties file to print the branch information using message source.
mymessages.properties
1 |
bank.information=HDFC Branch code : {0} and Address : {1} |
MessageSourceDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.j2eereference.spring.messageSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MessageSourceDemo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); System.out.println(context.getMessage("bank.information",new Object[]{"0001","Bangalore , India"},"Default Greeting", null)); } } |
Output:
1 |
Branch code :0001 and Address : Bangalore , India |
Here the message is picked up from the properties file and it also does the parameter substitution.
Leave a Reply