Java Specification Request JSR 250
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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="pointC" class="com.mugil.shapes.Point"> <property name="x" value="20"/> <property name="y" value="0"/> </bean> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="constant.properties"></property> </bean> <bean class="com.mugil.shapes.Circle" id="circleId"> </bean> <context:annotation-config/> </beans>
circle.java
public class Circle implements Shape{ private Point center; @Override public void drawShape() { System.out.println("Shape of Circle "); System.out.println("Center of Cirlce "+ getCenter().getX() + " - " + getCenter().getY()); } public Point getCenter() { return center; } @Resource(name="pointC") public void setCenter(Point center) { this.center = center; } @PostConstruct public void initilizeMe() { System.out.println("Initialized"); } @PreDestroy public void destroyMe() { System.out.println("Derstroyed"); } }
If name is not specified after the @Resource it will look for same name matching bean in the spring.xml file.
@PostConstruct and @PreDestroy will be called when circle bean is initialized and destroyed
Defining Bean using Annotations
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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package="com.mugil.shapes"/> </beans>
circle.java
@Component public class Circle implements Shape { . . }
drawingApp.java
public class DrawingApp { public static void main(String[] args) { AbstractApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml"); objContext.registerShutdownHook(); Shape objShape = (Shape)objContext.getBean("circle"); . . }
Using MessageSource
spring.xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>constant</value> </list> </property> </bean>
constant.properties
TestMessage=Mugil
DrawingApp.java
AbstractApplicationContext objContext = new ClassPathXmlApplicationContext("spring1.xml"); System.out.println(objContext.getMessage("TestMessage", null, "Default Message", null));
Output
Mugil
Using instance of Message Source in Bean Class
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-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>constant</value> </list> </property> </bean> <context:annotation-config/> <context:component-scan base-package="com.mugil.shapes"/> </beans>
circle.java
@Component public class Circle implements Shape{ private Point center; @Autowired private MessageSource messageSource; @Override public void drawShape() { System.out.println("Shape of Circle "); System.out.println(this.messageSource.getMessage("TestMessage", null, "Vannan", null)); } public Point getCenter() { return center; } @Resource(name="pointC") public void setCenter(Point center) { this.center = center; } public MessageSource getMessageSource() { return messageSource; } public void setMessageSource(MessageSource messageSource) { this.messageSource = messageSource; } }
this.messageSource.getMessage(“TestMessage”, null, “Vannan”, null)
Variable substitution in Message Source
circle.java
@Component public class Circle implements Shape{ private Point center; @Autowired private MessageSource messageSource; @Override public void drawShape() { System.out.println("Center of Cirlce"); System.out.println(this.messageSource.getMessage("TestMessage2", new Object[]{center.getX(), center.getY()}, "Vannan", null)); } . . . }
this.messageSource.getMessage(“TestMessage2”, new Object[]{center.getX(), center.getY()}, “Vannan”, null)
constant.properties
TestMessage=Mugil TestMessage2=Center of Circle : ({0}, {1})