【JavaEE】Spring核心与设计思想(控制反转式程序演示、IoC、DI)

一、什么是Spring?

通常所说的 Spring 指的是 Spring FrameworkSpring 框架),它是⼀个开源框架,有着活跃⽽庞⼤的社区,这就是它之所以能⻓久不衰的原因。Spring ⽀持⼴泛的应⽤场景,它可以让 Java 企业级的应⽤程序开发起来更简单。

Spring的定义可以用一句话概括:Spring 是包含了众多⼯具⽅法的 IoC 容器。

1.1 什么是容器?

容器是⽤来容纳某种物品的(基本)装置。 ——来⾃:百度百科

1.2 什么是IoC?

上面说了Spring是一个IoC容器

这里的IoC指的是 Inversion of Control(“控制反转”)

也就是说,Spring是一个“控制反转”的容器

1.3 Inversion of Control(“控制反转”)

为了方便理解,我们通过传统代码控制反转式的代码进行对比

假如,我们现在构建⼀辆“⻋”的程序,我们的实现思路是这样的:
在这里插入图片描述

1.3.1 传统代码实现:

public class IoCTest {
    public static void main(String[] args) {
        Car car = new Car();
        car.init();
    }
    static class Car {
        public void init() {
            //依赖车身
            Framework framework = new Framework();
            framework.init();
        }
    }
    static class Framework {
        public void init() {
            Bottom bottom = new Bottom();
            bottom.init();
        }
    }
    static class Bottom {
        public void init() {
            Tire tire = new Tire();
            tire.init();
        }
    }
    static public class Tire {
        private int size = 30;
        public void init() {
            System.out.println("轮胎尺寸" + size);
        }
    }
}

代码缺陷:
以上程序中,轮胎的尺⼨的固定的,然⽽随着对的⻋的需求量越来越⼤,个性化需求也会越来越多,这时候我们就需要加⼯多种尺⼨的轮胎,那这个时候就要对上⾯的程序进⾏修改了,修改后的代码如下所示:

public class IoCTest {
    public static void main(String[] args) {
        Car car = new Car(20);
        car.run();
    }
    static class Car {
        private Framework framework;
        public  Car(int size) {
            framework = new Framework(size);
        }
        public void run() {
            //依赖车身
            framework.init();
        }
    }
    static class Framework {
        private Bottom bottom;
        public Framework(int size) {
            bottom = new Bottom(size);
        }
        public void init() {
            bottom.init();
        }
    }
    static class Bottom {
        private Tire tire;
        public Bottom(int size) {
            tire = new Tire(size);
        }
        public void init() {
            tire.init();
        }
    }
    static class Tire {
        private int size;
        public Tire(int size) {
            this.size = size;
        }
        public void init() {
            System.out.println("轮胎尺⼨:" + size);
        }

    }
}

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

我们可以尝试不在每个类中⾃⼰创建下级类,如果⾃⼰创建下级类就会出现当下级类发⽣改变操作,⾃⼰也要跟着修改。
此时,我们只需要将原来由⾃⼰创建的下级类,改为传递的⽅式(也就是注⼊的⽅式),因为我们不需要在当前类中创建下级类了,所以下级类即使发⽣变化(创建或减少参数),当前类本身也⽆需修改任何代码,这样就完成了程序的解耦

1.3.2 控制反转式程序开发:

基于以上思路,我们把调⽤汽⻋的程序示例改造⼀下,把创建⼦类的⽅式,改为注⼊传递的⽅式,具体实现代码如下:

import javax.rmi.CORBA.Tie;

public class IoCTest {
    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.run();
    }

    static class Car {
        private Framework framework;
        public Car(Framework framework) {
            this.framework = framework;
        }
        public void run() {
            framework.init();
        }
    }

    static class Framework {
        private Bottom bottom;
        public Framework(Bottom bottom) {
            this.bottom = bottom;
        }
        public void init() {
            bottom.init();
        }
    }

    static class Bottom {
        private Tire tire;
        public Bottom(Tire tire) {
            this.tire = tire;
        }
        public void init() {
            tire.init();
        }
    }

    static class Tire {
        private int size;
        public Tire(int size) {
            this.size = size;
        }
        public void init() {
            System.out.println("轮胎: " + size);
        }
    }
}

在这里插入图片描述

1.3.3 对比

在这里插入图片描述

1.4 理解IoC

在这里插入图片描述

1.5 DI

DI(Dependency Injection”依赖注入“
在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>