Spring入门之一-------实现一个简单的IoC

一、场景模拟

public interface Human {

    public void goHome();

}
Human:人类,下班了该回家啦
public interface Car {

    void start();

    void stop();

    void turnLeft();

    void turnRight();

}
Car:汽车,可以启动、停止、左转、右转
public class AudiCar implements Car{

    public void start() {
        System.out.println(this.getClass().getSimpleName() + ": start !!!");
    }

    public void stop() {
        System.out.println(this.getClass().getSimpleName() + ": stop !!!");
    }

    public void turnLeft() {
        System.out.println(this.getClass().getSimpleName() + ": turnLeft !!!");
    }

    public void turnRight() {
        System.out.println(this.getClass().getSimpleName() + ": turnRight !!!");
    }
}
AudiCar:奥迪汽车,实现Car
public class BuickCar implements Car{

    public void start() {
        System.out.println(this.getClass().getSimpleName() + ": start !!!");
    }

    public void stop() {
        System.out.println(this.getClass().getSimpleName() + ": stop !!!");
    }

    public void turnLeft() {
        System.out.println(this.getClass().getSimpleName() + ": turnLeft !!!");
    }

    public void turnRight() {
        System.out.println(this.getClass().getSimpleName() + ": turnRight !!!");
    }
}
BuickCar:别克汽车,实现Car
public abstract class HumenWithCar implements Human{

    protected final Car car;

    public HumenWithCar(Car car) {
        this.car = car;
    }

    public abstract void goHome();

}
HumenWithCar有车一族,实现Human,拥有Car属性
public class Zhang3Human extends HumenWithCar{

    public Zhang3Human(Car car) {
        super(car);
    }

    public void goHome() {
        car.start();
        car.turnLeft();
        car.stop();
    }
}
Zhang3Human:张三,有车一族
public class Li4Human extends HumenWithCar{

    public Li4Human(Car car) {
        super(car);
    }

    public void goHome() {
        car.start();
        car.turnRight();
        car.stop();
    }
}
Li4Human:李四,有车一族

二、定义IoC容器

public class IocContrainner {

    /**
     * 用来存储beans,String为bean的id,Object为bean本身
     */
    private final Map<String, Object> beans = new HashMap<String, Object>();

    /**
     * 根据beanId获取一个bean
     * @param beanId beanId
     * @return 返回bean
     */
    public Object getBean(String beanId) {
        return beans.get(beanId);
    }

    /**
     * 委托IoC容器创建一个bean
     * @param clazz 要创建的bean的class
     * @param beanId beanId
     * @param paramBeanIds 要创建的bean的class的构造方法所依赖的bean
     */
    public void registerBean(Class<?> clazz, String beanId, String... paramBeanIds)  {
        Object[] paramValues = new Object[paramBeanIds.length];
        for (int i = 0; i < paramBeanIds.length; i++) {
            Object paramValue = getBean(paramBeanIds[i]);
            if (paramValue == null) {
                throw new RuntimeException("Can't find bean of id: " + paramBeanIds[i]);
            }
            paramValues[i] = paramValue;
        }

        Object bean = null;
        // 获取并循环所有的构造方法
        Constructor<?>[] constructors = clazz.getConstructors();
        for (Constructor<?> constructor : constructors) {
            try {
                bean = constructor.newInstance(paramValues);
            } catch (InstantiationException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }
        if (bean == null) {
            throw new RuntimeException("找不到合适的构造方法:" + paramBeanIds);
        }
        beans.put(beanId, bean);
    }
}

三、测试

public class IocContrainnerTest {

    private final IocContrainner iocContrainner = new IocContrainner();

    @Before
    public void before() {
        iocContrainner.registerBean(AudiCar.class, "audi");
        iocContrainner.registerBean(PorscheCar.class, "porsche");
        iocContrainner.registerBean(BuickCar.class, "buick");
        iocContrainner.registerBean(Zhang3Human.class, "zhang3", "audi");
        iocContrainner.registerBean(Li4Human.class, "li4", "porsche");
    }

    @Test
    public void test() {
        Human zhang3 = (Human) iocContrainner.getBean("zhang3");
        zhang3.goHome();
        Human li4 = (Human) iocContrainner.getBean("li4");
        li4.goHome();
    }

}

 

posted @ 2018-11-30 16:21  柚子苹果果  阅读(333)  评论(0编辑  收藏  举报