package com.dineshonjava.sdnext.jsr.tutorial; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource; public class Circle { private Point center; @Resource(name="pointB") public void setCenter(Point center) { this.center = center; } public void draw() { System.out.println("Circle is drawn of center ("+center.getX()+", "+center.getY()+")"); } @PostConstruct public void initializeCircle() { //populates the circle data cache upon initialization... System.out.println("Init of Circle"); } @PreDestroy public void destroyCircle() { //clears the circle related cache upon destruction.. System.out.println("Destroy of Circle"); } }Point.java
package com.dineshonjava.sdnext.jsr.tutorial; public class Point { private int x; private int y; /** * @return the x */ public int getX() { return x; } /** * @param x the x to set */ public void setX(int x) { this.x = x; } /** * @return the y */ public int getY() { return y; } /** * @param y the y to set */ public void setY(int y) { this.y = y; } }DrawingApp.java
package com.dineshonjava.sdnext.jsr.tutorial; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * @author Dinesh * */ public class DrawingApp { /** * @param args */ public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); context.registerShutdownHook(); Circle circle = (Circle) context.getBean("circle"); circle.draw(); } }
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans"> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Circle" id="circle"></bean> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Point" id="pointA"> <property name="x" value="0"></property> <property name="y" value="0"></property> </bean> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Point" id="pointB"> <property name="x" value="-20"></property> <property name="y" value="0"></property> </bean> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Point" id="pointC"> <property name="x" value="20"></property> <property name="y" value="0"></property> </bean> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Point" id="center"> <property name="x" value="10"></property> <property name="y" value="10"></property> </bean> <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"></bean> </beans>
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:security="http://www.springframework.org/schema/security" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans"> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Circle" id="circle"></bean> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Point" id="pointA"> <property name="x" value="0"></property> <property name="y" value="0"></property> </bean> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Point" id="pointB"> <property name="x" value="-20"></property> <property name="y" value="0"></property> </bean> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Point" id="pointC"> <property name="x" value="20"></property> <property name="y" value="0"></property> </bean> <bean class="com.dineshonjava.sdnext.jsr.tutorial.Point" id="center"> <property name="x" value="10"></property> <property name="y" value="10"></property> </bean> <context:annotation-config></context:annotation-config> </beans>
Labels: Spring3.0