In the previous blog , the class datatype variables are injected using the Spring framework. However, in this blog, we will be injecting Object-type variable using spring.
Let's say, Drawing App class still remains same,
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.Draw();
}
}
Create a new class Point, which is the intended class variable of object type, which we will be injecting using spring,
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;
}
}
Let's now create the Triangle class, which has three Point variables defined in it which is of Object type, as below,
public class Triangle {
public Triangle() {
}
private String type;
private int height;
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("PointA: (" + getPointA().getX() + "," + getPointA().getY()+")");
System.out.println("PointB: (" + getPointB().getX() + "," + getPointB().getY()+")");
System.out.println("PointC: (" + getPointC().getX() + "," + getPointC().getY()+")");
}
}
In order to inject these Object type class variables, in spring.xml
1)we need to define 3 Point beans as individual beans
2)refer the defined point beans as Variables of "triangle" bean using the "ref" attribute of <property> tag
as below,
<beans>
<bean id="triangle" class="com.project.springDemo.Triangle">
<property name="pointA" ref="Bean-pointA"/>
<property name="pointB" ref="Bean-pointB"/>
<property name="pointC" ref="Bean-pointC"/>
</bean>
<bean id="Bean-pointA" class="com.project.springDemo.Point">
<property name="x" value="10"/>
<property name="y" value="100"/>
</bean>
<bean id="Bean-pointB" class="com.project.springDemo.Point">
<property name="x" value="20"/>
<property name="y" value="200"/>
</bean>
<bean id="Bean-pointC" class="com.project.springDemo.Point">
<property name="x" value="30"/>
<property name="y" value="300"/>
</bean>
</beans>
Conclusion:
Though the 3 Point objects are initialized with their respective variable instantiations, there is only one reference initiation made to the Triangle class in the DrawingApp class which is the beauty of the Spring framework.
ie,
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.Draw();
Let's say, Drawing App class still remains same,
public class DrawingApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.Draw();
}
}
Create a new class Point, which is the intended class variable of object type, which we will be injecting using spring,
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;
}
}
Let's now create the Triangle class, which has three Point variables defined in it which is of Object type, as below,
public class Triangle {
public Triangle() {
}
private String type;
private int height;
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("PointA: (" + getPointA().getX() + "," + getPointA().getY()+")");
System.out.println("PointB: (" + getPointB().getX() + "," + getPointB().getY()+")");
System.out.println("PointC: (" + getPointC().getX() + "," + getPointC().getY()+")");
}
}
In order to inject these Object type class variables, in spring.xml
1)we need to define 3 Point beans as individual beans
2)refer the defined point beans as Variables of "triangle" bean using the "ref" attribute of <property> tag
as below,
<beans>
<bean id="triangle" class="com.project.springDemo.Triangle">
<property name="pointA" ref="Bean-pointA"/>
<property name="pointB" ref="Bean-pointB"/>
<property name="pointC" ref="Bean-pointC"/>
</bean>
<bean id="Bean-pointA" class="com.project.springDemo.Point">
<property name="x" value="10"/>
<property name="y" value="100"/>
</bean>
<bean id="Bean-pointB" class="com.project.springDemo.Point">
<property name="x" value="20"/>
<property name="y" value="200"/>
</bean>
<bean id="Bean-pointC" class="com.project.springDemo.Point">
<property name="x" value="30"/>
<property name="y" value="300"/>
</bean>
</beans>
Conclusion:
Though the 3 Point objects are initialized with their respective variable instantiations, there is only one reference initiation made to the Triangle class in the DrawingApp class which is the beauty of the Spring framework.
ie,
Triangle triangle = (Triangle) context.getBean("triangle");
triangle.Draw();
No comments:
Post a Comment