Java EE --- Spring

x33g5p2x  于2022-05-28 转载在 Java  
字(3.1k)|赞(0)|评价(0)|浏览(220)

1. 什么是 Spring

Spring 指的是 Spring Framework (Spring 框架), Spring 是一个开源的框架.
Spring 是包含了众多工具方法的 IoC 容器.

1.1 什么是容器

List/Map -> 数据存储容器
Tomcat -> Web 容器
Spring -> IoC 容器

1.2 什么是 IoC

IoC, Inversion of Control.译为 控制反转, 控制的是一个一个类, 反转就是类的生命周期不由程序员来控制了, 而是交给这个 IoC 容器来控制.

1.2.1 观看传统开发的代码

这就是传统开发的代码, 这个代码造的车, 只能固定大小.

/**
 * 造一个车
 */
public class MakeCar {
    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 class Tire {
        private int size = 30;
        public void init() {
            System.out.println("轮胎尺寸: " + size);
        }
    }
}

当我们需要搞不同尺寸的车的时候, 就需要修改代码

/**
 * 造一个车
 */
public class MakeCar {
    public static void main(String[] args) {
        Car car = new Car(20);
        car.init();
    }

    /**
     * 车类
     */
    static class Car {
        private Framework framework;
        public Car(int size){
            framework = new Framework(size);
        }
        public void init() {
            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.2.2 传统开发代码的缺陷

观察这两个代码. 发现 想要造不同的车的时候, 当底层发射改动, 上层就都需要改变.
当最底层的类被修改时, 那么整个调用链上的所有类都需要被修改
这就是 代码的耦合性太高了.

1.2.3 解决办法

我们打造⼀辆完整的汽⻋,如果所有的配件都是⾃⼰造,那么当客户需求发⽣改变的时候,⽐如轮胎的尺⼨不再是原来的尺⼨了,那我们要⾃⼰动⼿来改了,但如果我们是把轮胎外包出去,那么即使是轮胎的尺⼨发⽣变变了,我们只需要向代理⼯⼚下订单就⾏了,我们⾃身是不需要出⼒的。

/**
 * 造一辆车
 */
public class MakeNewCar {
    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();
    }

    /**
     * 车类
     */
    static class Car{
        private Framework framework;
        public Car(Framework framework){
            this.framework = framework;
        }
        public void init() {
            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);
        }
    }
}

这样代码之后, 想要再去修改车的尺寸颜色…,这些属性的时候, 整个调用链就不需要都改变了. 这样就完成了代码之间的解耦.

例如 想要尺寸为20 颜色为红色的车轮

1.2.4 对比总结

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

改进之后的控制权发生的反转,不再是上级对象创建并控制下级对象了,而是下级对象把注入将当前对象中,下级的控制权不再由上级类控制了,这样即使下级类发生任何改变,当前类都是不受影响的,这就是典型的控制反转

1.3 什么是 DI

DI, Dependency Injection. 译为依赖注入
依赖注入, 就是在IoC容器 在运行的时候, 动态的将某种依赖关系注入到对象之中.

IoC 是“⽬标”也是⼀种思想,⽽⽬标和思想只是⼀种指导原则,最终还是要有可⾏的落地⽅案,⽽ DI 就属于具体的实现

2. 总结

2.1 Spring 是什么?如何理解 Spring?

Spring 就是一个包含了众多工具的 IoC 容器.
包含了两个最核心最关键的功能:

  1. 将对象存入到 Spring 中.
  2. 从Spring 中获取到对象

2.2 IoC 和 DI 是啥?有什么区别?

IoC, Inversion of Control, 译为 控制反转. 使用控制反转的思路 ,可以实现依赖类之间的解耦,让我们不必在去关心依赖类的具体实现, 以及生命周期, 我们只需要在使用他的时候, 把他注入进来就行.
 
DI, Denpendency Injection, 译为 依赖注入. 是一种 Ioc 具体实现手段, 指的是在 IoC 容器运行的期间, 动态将某种依赖关系注入到对象中.
 
区别: IoC指导思想, DI具体的实现

2.3 Spring 最核心的功能是啥?

  1. 将对象存储到 Spring 当中
  2. 从 Spring 当中 取出对象.

相关文章