gudii9
asked on
spring injection of objects
Hi,
I am running below example on spring injection of objects
my code looks as below
My question is how is object injection is different compared to injecting primitives.
How it is printing point values are they primitives or objects?
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
selecttwo.jpg
I am running below example on spring injection of objects
my code looks as below
spring.xml
<?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" [
<!ENTITY contextInclude SYSTEM "org/springframework/web/portlet/con text/WEB-I NF/context Include.xm l">
]>
<beans>
<bean id="triangle" class="org.gp.gpr.Trianglebj">
<property name="pointA" ref="zeroPoint"/>
<property name="pointB" ref="point2"/>
<property name="pointC" ref="point3"/>
</bean>
<bean id="zeroPoint" class="org.gp.gpr.Point">
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>
<bean id="point2" class="org.gp.gpr.Point">
<property name="x" value="20"/>
<property name="y" value="0"/>
</bean>
<bean id="point3" class="org.gp.gpr.Point">
<property name="x" value="-200"/>
<property name="y" value="0"/>
</bean>
</beans>
client class looks as below
package org.gp.gpr;
import org.springframework.beans.factory.Be anFactory;
import org.springframework.beans.factory.xm l.XmlBeanF actory;
import org.springframework.context.Applicat ionContext ;
import org.springframework.context.support. ClassPathX mlApplicat ionContext ;
import org.springframework.core.io.ClassPat hResource;
import org.springframework.core.io.FileSyst emResource ;
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"));
ApplicationContext context=new ClassPathXmlApplicationContext("spri ng.xml");
Trianglebj trianglebj =(Trianglebj) context.getBean("triangle");
trianglebj.draw();
}
}
triangle object looks as below
package org.gp.gpr;
public class Trianglebj {
private Point pointA;
private Point pointB;
private Point pointC;
public void draw(){
// System.out.println(getType()+"triang le drawed"+getHeight());
System.out.println("PointA=("+getPoi ntA().getX ()+","+get PointA().g etY()+")") ;
System.out.println("PointB=("+getPoi ntB().getX ()+","+get PointB().g etY()+")") ;
System.out.println("PointC=("+getPoi ntC().getX ()+","+get PointC().g etY()+")") ;
}
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;
}
}
Point.java looks as below
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;
}
My question is how is object injection is different compared to injecting primitives.
How it is printing point values are they primitives or objects?
Please advise. Any ideas, resources, sample code highly appreciated. thanks in advance
selecttwo.jpg
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Agree with GuruJava and girionis.