Spring offer awareness with beans and Applicationcontext.
BeanNameAware interface provide to know bean information like name aware(know who you are).
ApplicationContextAware interface provide to know context information like context aware(Know where you live).
public setBeanName(String bean);
this method contain the name of the bean which is implement the interface BeanNameAware
public void setApplicationContext(ApplicationContext context)
this contain the container of context information where that bean exist.necessary to implement that class with ApplicationContextAware interface.
let illustrate example-
class Point.java
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="point1" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="draw" class="com.test.Draw">
</bean>
<bean id="triangle" class="com.test.Triangle" autowire="autodetect">
</bean>
</beans>
Output is=
BeanNameAware interface provide to know bean information like name aware(know who you are).
ApplicationContextAware interface provide to know context information like context aware(Know where you live).
public setBeanName(String bean);
this method contain the name of the bean which is implement the interface BeanNameAware
public void setApplicationContext(ApplicationContext context)
this contain the container of context information where that bean exist.necessary to implement that class with ApplicationContextAware interface.
let illustrate example-
class Point.java
package com.test;
public class Point {
private int x,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;
}
}
class Triangle.javapublic class Point {
private int x,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;
}
}
package com.test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Triangle implements ApplicationContextAware,BeanNameAware{
private Point point1;
private Draw dr;
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public void show()
{
System.out.println("Ponit1 x="+point1.getX()+"y="+point1.getY());
dr.draw();
}
public Draw getDr() {
return dr;
}
public void setDr(Draw dr) {
this.dr = dr;
}
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
System.out.println("context is="+context.toString());
}
@Override
public void setBeanName(String bean) {
System.out.println("bean name is="+bean);
}
}
class Draw.javaimport org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Triangle implements ApplicationContextAware,BeanNameAware{
private Point point1;
private Draw dr;
public Point getPoint1() {
return point1;
}
public void setPoint1(Point point1) {
this.point1 = point1;
}
public void show()
{
System.out.println("Ponit1 x="+point1.getX()+"y="+point1.getY());
dr.draw();
}
public Draw getDr() {
return dr;
}
public void setDr(Draw dr) {
this.dr = dr;
}
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
System.out.println("context is="+context.toString());
}
@Override
public void setBeanName(String bean) {
System.out.println("bean name is="+bean);
}
}
package com.test;
public class Draw {
public void draw()
{
System.out.println("Triangle Drawn");
}
}
class Main.javapublic class Draw {
public void draw()
{
System.out.println("Triangle Drawn");
}
}
package com.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri=(Triangle) context.getBean("triangle");
tri.show();
}
}
Spring.xmlimport org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle tri=(Triangle) context.getBean("triangle");
tri.show();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="point1" class="com.test.Point">
<property name="x" value="10"></property>
<property name="y" value="20"></property>
</bean>
<bean id="draw" class="com.test.Draw">
</bean>
<bean id="triangle" class="com.test.Triangle" autowire="autodetect">
</bean>
</beans>
Output is=
bean name is=triangle
context is=org.springframework.context.support.ClassPathXmlApplicationContext@1d6096: startup date [Mon Jun 25 10:38:24 IST 2012]; root of context hierarchy
Ponit1 x=10y=20
Triangle Drawn
context is=org.springframework.context.support.ClassPathXmlApplicationContext@1d6096: startup date [Mon Jun 25 10:38:24 IST 2012]; root of context hierarchy
Ponit1 x=10y=20
Triangle Drawn
For Further Reading,
0 comments:
Post a Comment