Monday, July 22, 2013

22-Using-MessageSource-To-Get-Text-From-Property-Files

To get messages from message.properties files your need to add below bean in xml file
      <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basenames">
                        <value>messages</value>
            </property>
      </bean>

If you want to display in main method, use below code to display property message
System.out.println(context.getMessage("greeting", null, "Default Greeting", null));

If you want to use in any of the other beans then you need configure “private MessageSource messageSource;” and need to Autowired in that class to read properties from .properties file.

      private MessageSource messageSource;

      System.out.println(this.messageSource.getMessage("greeting", null, "Default Greeting", null));

      public void setMessageSource(MessageSource messageSource) {
            this.messageSource = messageSource;
      }

      public MessageSource getMessageSource() {
            return messageSource;
      }

To add dynamic values to your property.
System.out.println(this.messageSource.getMessage(
"drawing.point",
new Object[]{center.getX(),center.getY()},
"Default point message",
Locale.ENGLISH));

drawing.point=Circle: ({0},{1})

DrawingApp.java
package org.yash.watertechsol;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class DrawingApp {
                public static void main(String[] args){
                                AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
                                Shape shape = (Shape)context.getBean("circle");
                                shape.draw();
                               
                                /* Params: String code, args-to send dynamic values, locale*/
                                System.out.println(context.getMessage("greeting", null, "Default Greeting", null));
                }
}

Circle.java
package org.yash.watertechsol;

import java.util.Locale;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import javax.annotation.processing.Messager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;

@Component
public class Circle implements Shape {

                private Point center;
                @Autowired
                private MessageSource messageSource;

                @Override
                public void draw() {
                                //System.out.println(this.messageSource.getMessage("drawing.circle", null, "Default circle", null));
                                System.out.println(this.messageSource.getMessage("drawing.point", new Object[]{center.getX(),center.getY()}, "Default point message", Locale.ENGLISH));
                                //System.out.println("Circle: ("+center.getX()+", "+center.getY()+")");
                                //System.out.println(this.messageSource.getMessage("greeting", null, "Default Greeting", null));
                }
               
                public Point getCenter() {
                                return center;
                }
               
                @Resource(name="pointC")
                public void setCenter(Point center) {
                                this.center = center;
                }
               

                public void setMessageSource(MessageSource messageSource) {
                                this.messageSource = messageSource;
                }

                public MessageSource getMessageSource() {
                                return messageSource;
                }
}

Shape.java
package org.yash.watertechsol;

public interface Shape {
      public void draw();
}

Triangle.java
package org.yash.watertechsol;


public class Triangle implements Shape{
     
      private Point pointA;
      private Point pointB;
      private Point pointC;
     
      public Point getPointA() {
            return pointA;
      }

      public void setPointA(Point pointA) {
            this.pointA = pointA;
      }

      public Point getPointB() {
            return pointB;
      }

      public void setPointB(Point pointB) {
            this.pointB = pointB;
      }

      public Point getPointC() {
            return pointC;
      }

      public void setPointC(Point pointC) {
            this.pointC = pointC;
      }

      public void draw(){
            System.out.println("Drawing Triangle");
            System.out.println("Point A = (" + getPointA().getX()+", "+getPointA().getY()+")");
            System.out.println("Point B = (" + getPointB().getX()+", "+getPointB().getY()+")");
            System.out.println("Point C = (" + getPointC().getX()+", "+getPointC().getY()+")");
      }

}

Point.java
package org.yash.watertechsol;

public class Point {
      private int x;
      private int y;
     
      public int getX() {
            return x;
      }
      public void setX(int x) {
            this.x = x;
      }
      public int getY() {
            return y;
      }
      public void setY(int y) {
            this.y = y;
      }
}

spring.xml
<?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: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-3.0.xsd">
           
      <!--
      <bean id="triangle" class="org.yash.watertechsol.Triangle">
            <property name="pointA" ref="pointA"></property>
            <property name="pointB" ref="pointB"></property>
            <property name="pointC" ref="pointC"></property>
      </bean>
      -->

      <!--
      <bean id="circle" class="org.yash.watertechsol.Circle">
            <property name="messageSource" ref="messageSource"></property>
      </bean>
       -->
     
      <bean id ="pointA" class="org.yash.watertechsol.Point">
            <property name="x" value="0" />
            <property name="y" value="0" />
      </bean>
     
      <bean id ="pointB" class="org.yash.watertechsol.Point">
            <property name="x" value="-20" />
            <property name="y" value="0" />
      </bean>

      <bean id ="pointC" class="org.yash.watertechsol.Point">
            <property name="x" value="0" />
            <property name="y" value="20" />
      </bean>

      <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
            <property name="basenames">
                        <value>messages</value>
            </property>
      </bean>
     
      <context:annotation-config />
      <context:component-scan base-package="org.yash.watertechsol"></context:component-scan>

</beans>

messages_en.properties
greeting=Hello!
drawing.circle=Drawing Circle
drawing.point=Circle: ({0},{1})

messages_fr.properties
greeting=Hello French!
drawing.circle=Drawing Circle French

drawing.point=Circle: ({0},{1}) French

No comments:

Post a Comment