package org.zcs.spring;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;publicclassDrawingApp{publicstaticvoidmain(String[] args){// TODO Auto-generated method stub//BeanFactory factory = new FileSystemXmlApplicationContext("src/spring.xml");//Triangle triangle = new Triangle();
ApplicationContext context =newClassPathXmlApplicationContext("spring.xml");
Triangle triangle =(Triangle)context.getBean("triangle");
triangle.draw();}}
Triangle.java
package org.zcs.spring;import java.util.List;publicclassTriangle{private List<Point> list ;public List<Point>getList(){return list;}publicvoidsetList(List<Point> list){this.list = list;}publicvoid draw (){for(Point p : list){
System.out.println("This is Point: ("+p.getX()+","+(p.getY())+")");}}}
spring.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "triangle" class= "org.zcs.spring.Triangle" name = "triangle-name">
<propertyname="list"><list>
<ref bean= "zeroPoint"/>
<ref bean= "twoPoint"/>
<ref bean= "threePoint"/>
</list></property></bean>
<bean id= "zeroPoint" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "0"></property></bean>
<bean id= "twoPoint" class = "org.zcs.spring.Point">
<property name="x" value = "20"></property>
<property name="y" value = "0"></property></bean>
<bean id= "threePoint" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "20"></property></bean></beans>
2.bean 的自动装入
DrawingApp.java
package org.zcs.spring;import org.springframework.beans.factory.BeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;publicclassDrawingApp{publicstaticvoidmain(String[] args){// TODO Auto-generated method stub//BeanFactory factory = new FileSystemXmlApplicationContext("src/spring.xml");//Triangle triangle = new Triangle();
ApplicationContext context =newClassPathXmlApplicationContext("spring.xml");
Triangle triangle =(Triangle)context.getBean("triangle");
triangle.draw();}}
Triangle.java
package org.zcs.spring;import java.util.List;publicclassTriangle{private Point pointA;private Point pointB;private Point pointC;public Point getPointA(){return pointA;}publicvoidsetPointA(Point pointA){this.pointA = pointA;}public Point getPointB(){return pointB;}publicvoidsetPointB(Point pointB){this.pointB = pointB;}public Point getPointC(){return pointC;}publicvoidsetPointC(Point pointC){this.pointC = pointC;}publicvoid draw (){
System.out.println("This is PointA: ("+getPointA().getX()+","+getPointA().getY()+")");
System.out.println("This is PointB: ("+getPointB().getX()+","+getPointB().getY()+")");
System.out.println("This is PointC: ("+getPointC().getX()+","+getPointC().getY()+")");}}
spring.xml
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id = "triangle" class= "org.zcs.spring.Triangle" autowire= "byName">
</bean>
<bean id= "pointA" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "0"></property></bean>
<bean id= "pointB" class = "org.zcs.spring.Point">
<property name="x" value = "20"></property>
<property name="y" value = "0"></property></bean>
<bean id= "pointC" class = "org.zcs.spring.Point">
<property name="x" value = "0"></property>
<property name="y" value = "20"></property></bean></beans>