Spring IOC 举例

Spring IOC

Spring是一个 IOC 容器

IOC

IOC = Inversion of Control => 控制反转

传统程序

加入现在构建一辆"车" 的程序

构建一辆车(Car Class) , 然而车需要依赖车身 (FrameWork Class) ,车身依赖底盘(Bottom Class) , 底盘依赖轮胎(Tire Class)

实现代码如下 :

public class Car {
    private Framework framework;
    
    public Car(int size) {
        framework = new Framework(size);
    }
    public void init() {
        System.out.println("do car");
        framework.init();
    }
    public static void main(String[] args) {
        Car car = new Car(20);
        car.init();
    }
}
public class Framework {
    private Bottom bottom;

    public Framework(int size) {
        bottom = new Bottom(size);
    }

    public void init() {
        System.out.println("do framework");
        bottom.init();
    }
}
public class Bottom {
    private Tire tire;

    public Bottom(int size) {
        tire = new Tire(size);
    }

    public void init() {
        System.out.println("do bottom");
        tire.init();
    }
}
public class Tire {
    private int size = 17;

    public Tire(int size) {
        this.size = size;
    }

    public void init() {
        System.out.println("size -> " + size);
    }
}

从以上 代码可以看出 , 程序的问题是 :当底层代码改动后,整个调用链上的所有代码都需要修改.

如何解决上述问题 ?

只需要将原来由自己创建的下级类 , 改为传递的方式(也就是注入的方式) , 因为不需要在当前类中创建下级类了 , 所以下级类即使发生变化(创建或减少参数) , 当前类本身也无需修改任何代码 , 这样就完成了程序的解耦 .

控制反转程序

public class Test {
    public static void main(String[] args) {
        Tire tire = new Tire(20, "黑色");
        Bottom bottom = new Bottom(tire);
        Framework framework = new Framework(bottom);
        Car car = new Car(framework);
        car.init();
    }
}
public class Car {
    private Framework framework;

    public Car(Framework framework) {
        this.framework = framework;
//        framework = new Framework();
    }

    public void init() {
        System.out.println("do car...");
        framework.init();
    }
}
public class Framework {
    private Bottom bottom;

    public Framework(Bottom bottom) {
        this.bottom = bottom;
    }

    public void init() {
        System.out.println("do framework...");
        bottom.init();
    }
}
public class Bottom {
    private Tire tire;

    public Bottom(Tire tire) {
        this.tire = tire;
    }

    public void init() {
        System.out.println("do bottom...");
        tire.init();
    }
}
public class Tire {
    private int size = 17;
    private String color = "红色";

    public Tire(int size, String color) {
        this.size = size;
    }

    public void init() {
        System.out.println("size -> " + size + " | color -> " + color);
    }
}

代码经过以上调整 , 无论底层怎么变化 , 整个调用链是不用做任何改变的 , 这样就完成了代码之间的解耦.

对比总结 :

在传统的代码中对象创建顺序是:Car -> Framework -> Bottom -> Tire
改进之后解耦的代码的对象创建顺序是:Tire -> Bottom -> Framework -> Car

通⽤程序的实现代码,类的创建顺序是反的,传统代码是 Car 控制并创建了
Framework,Framework 创建并创建了 Bottom,依次往下,⽽改进之后的控制权发⽣的反转,不再是上级对象创建并控制下级对象了,⽽是下级对象把注⼊将当前对象中,下级的控制权不再由上级类控制了,这样即使下级类发⽣任何改变,当前类都是不受影响的,这就是典型的控制反转,也就是 IoC 的实
现思想。

理解

Spring 是包含了多个工具方法的 Ioc 容器

Spring 的最基础的功能 :

  • 将对象存到容器中
  • 将对象从容器中取出来

将对象存放到容器中的好处 : 将对象存储到 IOC容器相当于将以后可能用的所有工具制作好都放到仓库中 , 需要的时候直接取就行了,用完再把它放回到仓库 . 而NEW 对象的方式就相当于 , 每次需要工具了,才现做,用完就扔掉也不会保存 , 下次使用的时候还得重新做,这就是IOC容器和普通程序开发的区别

Spring 是⼀个 IoC 容器,说的是对象的创建和销毁的权利都交给 Spring 来管理了,它本身⼜具备了存储对象和获取对象的能⼒。

Spring IOC 的优点

  1. 解耦
  2. 使用更加方便 (不需要手动创建,和关注这个对象背后的依赖关系)
  3. 更加高效
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值