以前对象之间的引用是通过new来调用实现,有了Spring IOC,我们可以把对象之间的引用交给他来管理,这样就把控制权交给了Spring,所以就叫做控制反转。
Spring IOC的实现用到了设计模式:简单工厂,他也是从简单工厂进化而来的,下面我们看看Spring的IOC是如何进化来的。
package org;
//抽象接口
interface Fruit{
public void eat();
}
//实现类A
class Apple implements Fruit{
public void eat(){
System.out.println("吃苹果。");
}
}
//实现类B
class Orange implements Fruit{
public void eat(){
System.out.println("吃橘子");
}
}
//工厂类
class Factory{
public static Fruit getInstance(String className){
Fruit f=null;
if(className.equals("apple")){
f=new Apple();
}
if(className.endsWith("orange")){
f=new Orange();
}
return f;
}
}
public class FactoryDemo02 {
public static void main(String args[]){
Fruit f=Factory.getInstance("apple");
f.eat();
}
}
package org1; interface Fruit { public void eat(); } class Apple implements Fruit { public void eat() { System.out.println("吃苹果。"); } } class Orange implements Fruit { public void eat() { System.out.println("吃橘子"); } } class Factory { public static Fruit getInstance(String className) { Fruit f = null; try { f = (Fruit) Class.forName(className).newInstance(); } catch (Exception e) { e.printStackTrace(); } return f; } } public class CopyOfFactoryDemo03 { public static void main(String args[]) { Fruit f = Factory.getInstance("org1.Apple"); f.eat(); } }
package org3; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; interface Fruit { public void eat(); } class Apple implements Fruit { public void eat() { System.out.println("吃苹果。"); } } class Orange implements Fruit { public void eat() { System.out.println("吃橘子"); } } class Factory { public static Fruit getInstance(String className) { Fruit f = null; try { f = (Fruit) Class.forName(className).newInstance(); } catch (Exception e) { e.printStackTrace(); } return f; } } class PropertiesOperate{ private Properties pro=null; private File file=new File("d:"+File.separator+"fruit.properties"); public PropertiesOperate(){ this.pro=new Properties(); if(file.exists()){ try { pro.loadFromXML(new FileInputStream(file)); } catch (Exception e) { e.printStackTrace(); } }else{ this.save(); } } private void save(){ this.pro.setProperty("apple","org3.Apple"); this.pro.setProperty("orange", "org3.Orange"); try { this.pro.storeToXML(new FileOutputStream(this.file),"Fruit"); } catch (Exception e) { e.printStackTrace(); } } public Properties getProperties(){ return this.pro; } } public class CopyOfFactoryDemo04 { public static void main(String args[]) { Properties pro=new PropertiesOperate().getProperties(); Fruit f= Factory.getInstance(pro.getProperty("apple")); f.eat(); } }
package test2; public class Person { private String name; private int age; private Grade grade; public String getName() { return name; } public Grade getGrade() { return grade; } public void setGrade(Grade grade) { this.grade = grade; } public void setName(String name) { this.name = name; } public void setAge(int age) { this.age = age; } public int getAge() { return age; } public int getTotleGrade() { return grade.getEnglish()+grade.getMath(); } }
package test2; public class Grade { private int math; private int english; public int getMath() { return math; } public void setMath(int math) { this.math = math; } public int getEnglish() { return english; } public void setEnglish(int english) { this.english = english; } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>//很多豆豆 <bean id="Person" class="test2.Person">//第一个豆豆,是一个Person类,id名字随便取,还要写上类的全名 <property name="name">//下面开始把这个类里面的所有属性列出来,并赋值,至于你说难道一定要赋值吗?我想可以,我刚学,不知道 <value>小龙</value>//这里的名字是通过程序里面的set来赋值的,不信你去掉程序里面相关的set,就出错了 </property> <property name="age"> <value>23</value> </property> <property name="grade">//这里有点特别,这个grade变量是一个对象,和一般的变量要区别对待 <ref local="Grade"/>//这里指向了本配置文件里面一个名字叫Grade(即id=Grade)的bean </property> </bean> <bean id="Grade" class="test2.Grade">//同上 <property name="math"> <value>99</value> </property> <property name="english"> <value>59</value> </property> </bean> </beans>
package test2; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import test.ExampleBean; public class Test { public static void main(String args[]){ Resource input = new ClassPathResource("test2/Bean.xml");//Bean.xml的路径 System.out.println("resource is:" + input); BeanFactory factory = new XmlBeanFactory(input);//把input扔到工厂里面去,这个工厂就能为你提供实例了(我也不知道能不能这样说) Person person =(Person) factory.getBean("Person");//你要一个叫Person的东西,那好,工厂就去找“Person"给你 Grade grade=(Grade)factory.getBean("Grade"); System.out.println("姓名:"+person.getName());//person可以调用里面相关的方法,就相当于new了一个Person一样 System.out.println("年龄:"+person.getAge()); System.out.println("数学成绩:"+grade.getMath()); System.out.println("英语成绩:"+grade.getEnglish()); System.out.println("数学,英语总成绩:"+person.getTotleGrade()); } }
原文:http://blog.csdn.net/jwdstef/article/details/18768633