【Basic computer】-----Java Spring :Injection Objects (注入对象篇)



Java Spring -----Injection Objects


【Abstract】


     Today we are continue to talk about the java and this time we are gonna to get in the injection Objects.

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.


    Here is the SpringMVC pattern, there maybe a button or label some kind of “Incoming request” send to “Front Controller” ,what does “Front Controller” do, and why there have two Controller, what is the difference?
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.





  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值