Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

spring internalization

Hi,

I am running below example on spring internalization
circle class is

package org.gp.gpr;

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
//@Component
//@Service
//@Repository
@Controller
public class Circle implements Shape {
private Point center;
@Autowired
private MessageSource messageSource;
	public MessageSource getMessageSource() {
	return messageSource;
}
public void setMessageSource(MessageSource messageSource) {
	this.messageSource = messageSource;
}
	public Point getCenter() {
	return center;
}
	@Resource
	//@Resource(name="pointC")
////@Required
//	@Autowired
//	@Qualifier("circleRelated")
public void setCenter(Point center) {
	this.center = center;
}
	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println(this.messageSource.getMessage("drawing.circle", null,"default drwning msg",null));
		System.out.println(this.messageSource.getMessage("drawing.point",new Object[] {center.getX(),center.getY()},"default drwning msg",null));
				
				
		}
	//	System.out.println("circle point ("+center.getX()+",,,"+center.getY()+")");

	//System.out.println(this.messageSource.getMessage("greeting", null,"default grtng",null));
//	}
	
	@PostConstruct
public void initializeCircle(){
	System.out.println("in circle initialize");
}
@PreDestroy
public void destroyCircle(){
	System.out.println("in circle destroy");
}
}



client class is

package org.gp.gpr;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

public class DrawingAppObj {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//	Triangle triangle=new Triangle();
		//	triangle.draw();


//		BeanFactory factory=new XmlBeanFactory(new FileSystemResource("spring.xml"));
//		AbstractApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
//		context.registerShutdownHook();
//		Trianglebj trianglebj	=(Trianglebj) context.getBean("triangle");
//	//	Trianglebj trianglebj	=(Trianglebj) context.getBean("triangle-alias");
//		trianglebj.draw();
		
		
	//	Circle circle	=(Circle) context.getBean("circle");
		//	Trianglebj trianglebj	=(Trianglebj) context.getBean("triangle-alias");
		//circle.draw();
//		ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
		AbstractApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
		context.registerShutdownHook();
		Shape shape=(Shape)context.getBean("circle");
//		Shape shape=(Shape)context.getBean("triangle");
				shape.draw();
		//	System.out.println(context.getMessage("greeting", null,"default grtng",null));
				
		
	}

}
















point class is

package org.gp.gpr;

public class Point {
private int x;
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;
}
private int y;

}


shape interface is

package org.gp.gpr;

public interface Shape {
public void draw();
}

triangle class is

package org.gp.gpr;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class Trianglebj implements Shape{


private Point pointA;
private Point pointB;
private Point pointC;
//private ApplicationContext context=null;

	public void draw(){

System.out.println("triangle draw");
	//	System.out.println(getType()+"triangle drawed"+getHeight());
	System.out.println("PointA=("+getPointA().getX()+","+getPointA().getY()+")");
	System.out.println("PointB=("+getPointB().getX()+","+getPointB().getY()+")");
	System.out.println("PointC=("+getPointC().getX()+","+getPointC().getY()+")");
		

	}

	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 myInit(){
		System.out.println("myinit() beans init called for triangle");	
	}
	
	public void cleanUp(){
		System.out.println("cleanUp() beans cleanUp called for triangle");	
	}

//	@Override
//	public void destroy() throws Exception {
//		// TODO Auto-generated method stub
//		System.out.println("destroy of DisposableBean called");
//		
//	}
//
//	@Override
//	public void afterPropertiesSet() throws Exception {
//		// TODO Auto-generated method stub
//		System.out.println("afterPropertiesSet of initializing bean called");
//		
//	}

	
}


mymessages property file is

greeting=hi all
drawing.circle==drawing circlee
drawing.point=circle point ({0},{1})


spring xmnl is

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
	xmlns:context="http://www.springframework.org/schema/context">


<!-- 
<bean id="triangle" class="org.gp.gpr.Trianglebj" autowire="byName">
<property name="pointA" ref="pointA"/>
<property name="pointB" ref="pointB"/>
<property name="pointC" ref="pointC"/>
</bean> -->

<bean id="pointA" class="org.gp.gpr.Point">
<qualifier value="circleRelated"/>
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>


<bean id="pointB" class="org.gp.gpr.Point">
<property name="x" value="20"/>
<property name="y" value="0"/>
</bean>

<bean id="center" class="org.gp.gpr.Point">
<property name="x" value="-200"/>
<property name="y" value="0"/>
</bean> 

<!-- <bean id="circle" class="org.gp.gpr.Circle">
<property name="center" ref="pointA"/>

</bean> -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>mymessages</value>
</list>
</property>
</bean>
<!-- <bean class="org.gp.gpr.DisplayNameBeanPostProcessor"/> -->
<bean class="org.gp.gpr.MyBeanFactoryPostProcessor"/> 
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name="location" value="pointsconfig.properties"></property>
</bean>
<!-- <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> -->
<!-- <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> -->

	<context:annotation-config/>
	<context:component-scan base-package="org.gp.gpr"/>
</beans>

points property file is

pointA.pointX=0
pointA.pointY=0

Open in new window


output is

Apr 1, 2013 10:46:50 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@31d520c4: startup date [Mon Apr 01 10:46:50 CDT 2013]; root of context hierarchy
Apr 1, 2013 10:46:50 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Apr 1, 2013 10:46:50 AM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
INFO: Loading properties file from class path resource [pointsconfig.properties]
my bean factory post processor invoked
Apr 1, 2013 10:46:50 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@536c79d7: defining beans [pointA,pointB,center,messageSource,org.gp.gpr.MyBeanFactoryPostProcessor#0,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,circle]; root of factory hierarchy
in circle initialize
=drawing circlee
circle point (-200,0)
in circle destroy



my question is how do i extend this application to support locale. Let us say if i want to support spanish also along with english how do i make chnages to reflect that.

Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
Avatar of GuruJava
GuruJava

hi gudii9,

this is simple and good example have look here ...
http://www.mkyong.com/spring-mvc/spring-mvc-internationalization-example/
ASKER CERTIFIED SOLUTION
Avatar of GuruJava
GuruJava

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial