Spring深入浅出(一)IOC详解(一)

2014-11-24 02:55:35 · 作者: · 浏览: 0

IOC即inverse of control 控制反转

以前对象之间的引用是通过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();
    }
}

反射+简单工厂

但是工厂类如果这样写的话,就有一个问题,如果增加了水果,比如香蕉,那么在工厂类里面也要进行相关的修改了,这样不合理,而java的反射机制可以解决这个问题
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();
	}
}

反射+简单工厂+xml配置文件

利用java的反射机制,就能动态的实例化各种类了。 但是这个程序还是存在一个问题,就是主函数这里需要填入一个完整的类名称,不够方便,所以要增加配置文件来简化
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();
    }
}

终极版本Spring IOC

加入配置文件问题就解决了,以后如果要增加新的水果类,都要在这个配置文件里面登记。这时我们可以说配置文件可以控制程序的执行,现在看起来有点像spring的ioc了。下面我们来看看Spring IOC是如何实现的。
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 v