Java Spring -----Injection Objects
【Abstract】
I am going to give you a demo and a view help you to understand it easily. Spring Framework is a Java platform that provides comprehensive infrastructure support for developing Java applications. Spring handles the infrastructure so you can focus on your application.
As we know ,long time ago ,we use about MVC ,up to right now, MVC is upgrade as usual like the society is update rapidly . Controller separate two as “Front Controller” and ”Controller”, Front Controller is going to deal with the model view , while “Controller” resolve the business logic.
As we can see in the drawing, incoming request message send to “Front Controller”,which Front Controller delegate the request to “Controller”, after that, Controller got the DB and send the business logic to Model,,,,,,,
So, what is the code,can you show off? Of course, the following is the code:
【DrawingApp.java】
package Daniel.tgb;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class DrawingApp {
public static void main(String[] args){
// Tringle triangle =new Tringle();
// BeanFactory factory=new XmlBeanfacotry(new FileSystemResource("spring.xml"));
ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
Triangle triangle =(Triangle)context.getBean("triangle");
triangle.draw();
}
}
【Point.java】
package Daniel.tgb;
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;
}
}
【Triangle.java】
package Daniel.tgb;
public class Triangle {
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() {
// TODO Auto-generated method stub
System.out.println("PointA=("+getPointA().getX()+","+getPointA().getY()+")");
System.out.println("PointB=("+getPointB().getX()+","+getPointB().getY()+")");
System.out.println("PointC=("+getPointC().getX()+","+getPointC().getY()+")");
}
}
【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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="triangle" class="Daniel.tgb.Triangle">
<property name="pointA" ref="Point1"/>
<property name="pointB" ref="Point2"/>
<property name="pointC" ref="Point3"/>
</bean>
<bean id="Point1" class="Daniel.tgb.Point">
<property name="x" value="0"/>
<property name="y" value="0"/>
</bean>
<bean id="Point2" class="Daniel.tgb.Point">
<property name="x" value="-20"/>
<property name="y" value="0"/>
</bean>
<bean id="Point3" class="Daniel.tgb.Point">
<property name="x" value="0"/>
<property name="y" value="20"/>
</bean>
</beans>
Here is the results :
Impression: Java applications -- a loose term that runs the gamut from constrained applets to n-tier server-side enterprise applications -- typically consist of objects that collaborate to form the application proper. Thus the objects in an application have dependencies on each other.