• 【Java EE】Spring核心思想(一)——IOC


    🎍Spring 是什么?

    通过前⾯的学习, 我们知道了Spring是⼀个开源框架, 他让我们的开发更加简单. 他⽀持⼴泛的应⽤场
    景, 有着活跃⽽庞⼤的社区, 这也是Spring能够⻓久不衰的原因.

    但是这个概念相对来说, 还是⽐较抽象.

    我们⽤⼀句更具体的话来概括Spring, 那就是: Spring 是包含了众多⼯具⽅法的 IoC 容器

    那问题来了,什么是容器?什么是 IoC 容器?接下来我们⼀起来看

    🎄什么是IoC呢?

    Spring 也是⼀个容器,Spring 是什么容器呢?Spring 是⼀个 IoC 容器。

    我们想想,之前课程我们接触的容器有哪些?
    • List/Map -> 数据存储容器
    Tomcat -> Web 容器

    IoC = Inversion of Control 翻译成中⽂是“控制反转”的意思,也就是说 Spring 是⼀个“控制反转”的容器,怎么理解这句话呢,我们先从以下示例开始

    接下来我们通过案例来了解⼀下什么是IoC
    需求: 造⼀辆⻋

    🌸传统程序开发

    我们的实现思路是这样的:

    先设计轮⼦(Tire),然后根据轮⼦的⼤⼩设计底盘(Bottom),接着根据底盘设计⻋⾝(Framework),最
    后根据⻋⾝设计好整个汽⻋(Car)。这⾥就出现了⼀个"依赖"关系:汽⻋依赖⻋⾝,⻋⾝依赖底盘,底
    盘依赖轮⼦.
    在这里插入图片描述
    最终程序的实现代码如下:

    public class NewCarExample {
     public static void main(String[] args) {
    Car car = new Car();
    car.run();
     }
     /**
     * 汽⻋对象
     */
     static class Car {
     private Framework framework;
     public Car() {
     framework = new Framework();
     System.out.println("Car init....");
     }
     public void run(){
     System.out.println("Car run...");
     }
     }
     /**
     * ⻋⾝类
     */
     static class Framework {
     private Bottom bottom;
     public Framework() {
     bottom = new Bottom();
     System.out.println("Framework init...");
     }
     }
     /**
     * 底盘类
     */
     static class Bottom {
     private Tire tire;
     public Bottom() {
     this.tire = new Tire();
     System.out.println("Bottom init...");
     }
     }
     /**
     * 轮胎类
     */
     static class Tire {
     // 尺⼨
     private int size;
     public Tire(){
     this.size = 17;
     System.out.println("轮胎尺⼨:" + size);
     }
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    🌸传统程序开发的缺陷

    这样的设计看起来没问题,但是可维护性却很低.

    接下来需求有了变更: 随着对的⻋的需求量越来越⼤, 个性化需求也会越来越多,我们需要加⼯多种尺
    ⼨的轮胎.

    那这个时候就要对上⾯的程序进⾏修改了,修改后的代码如下所⽰:

    public class NewCarExample {
     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);
     System.out.println("Car init....");
     }
     public void run(){
     System.out.println("Car run...");
     }
     }
     /**
     * ⻋⾝类
     */
     static class Framework {
     private Bottom bottom;
     public Framework(int size) {
     bottom = new Bottom(size);
     System.out.println("Framework init...");
     }
     }
     /**
     * 底盘类
     */
     static class Bottom {
     private Tire tire;
     public Bottom(int size) {
     this.tire = new Tire(size);
     System.out.println("Bottom init...");
     }
     }
     /**
     * 轮胎类
     */
     static class Tire {
     // 尺⼨
     private int size;
     public Tire(int size){
     this.size = size;
     System.out.println("轮胎尺⼨:" + size);
     }
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    从以上代码可以看出,以上程序的问题是:当最底层代码改动之后,整个调⽤链上的所有代码都需要
    修改.
    程序的耦合度⾮常⾼(修改⼀处代码, 影响其他处的代码修改)

    🌸如何解决传统程序的缺陷?

    我们可以尝试不在每个类中⾃⼰创建下级类,如果⾃⼰创建下级类就会出现当下级类发⽣改变操作,
    ⾃⼰也要跟着修改.

    此时,我们只需要将原来由⾃⼰创建的下级类,改为传递的⽅式(也就是注⼊的⽅式),因为我们不
    需要在当前类中创建下级类了,所以下级类即使发⽣变化(创建或减少参数),当前类本⾝也⽆需修
    改任何代码,这样就完成了程序的解耦.

    解耦指的是解决了代码的耦合性,耦合性也可以换⼀种叫法叫程序相关性。好的程序代码的耦合性(代码之间的相关性)是很低的,也就是代码之间要实现解耦这就好⽐我们打造⼀辆完整的汽⻋,如果所有的配件都是⾃⼰造,那么当客户需求发⽣改变的时候,⽐如轮胎的尺⼨不再是原来的尺⼨了,那我们要⾃⼰动⼿来改了,但如果我们是把轮胎外包出去,那么即使是轮胎的尺⼨发⽣变变了,我们只需要向代理⼯⼚下订单就⾏了,我们⾃身是不需要出⼒的。

    🌸控制反转式程序开发

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

    public class IocCarExample {
     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;
     System.out.println("Car init....");
     }
     public void run() {
     System.out.println("Car run...");
     }
     }
     static class Framework {
     private Bottom bottom;
     public Framework(Bottom bottom) {
     this.bottom = bottom;
     System.out.println("Framework init...");
     }
     }
     static class Bottom {
     private Tire tire;
     public Bottom(Tire tire) {
     this.tire = tire;
     System.out.println("Bottom init...");
     }
     }
     static class Tire {
     private int size;
     public Tire(int size) {
     this.size = size;
     System.out.println("轮胎尺⼨:" + size);
     }
     }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40

    代码经过以上调整,⽆论底层类如何变化,整个调⽤链是不⽤做任何改变的,这样就完成了代码之间
    解耦,从⽽实现了更加灵活、通⽤的程序设计了。

    🌸对比总结

    在传统的代码中对象创建顺序是:Car -> Framework -> Bottom -> Tire
    改进之后解耦的代码的对象创建顺序是:Tire -> Bottom -> Framework -> Car
    在这里插入图片描述
    我们发现了⼀个规律,通⽤程序的实现代码,类的创建顺序是反的,传统代码是 Car 控制并创建了
    Framework,Framework 创建并创建了 Bottom,依次往下,⽽改进之后的控制权发⽣的反转,不再是使⽤⽅对象创建并控制依赖对象了,⽽是把依赖对象注⼊将当前对象中,依赖对象的控制权不再由
    当前类控制了.

    这样的话, 即使依赖类发⽣任何改变,当前类都是不受影响的,这就是典型的控制反转,也就是 IoC 的实现思想。

    学到这⾥, 我们⼤概就知道了什么是控制反转了, 那什么是控制反转容器呢, 也就是IoC容器
    在这里插入图片描述

    🌲理解 Spring IoC

    Spring 是包含了多个⼯具⽅法的 IoC 容器
    具备的基础功能:

    • 将对象存⼊到容器;

    • 从容器中取出对象。

    也就是说学 Spring 最核⼼的功能,就是学如何将对象存⼊到 Spring 中,再从 Spring 中获取对象的过程

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

    从上⾯也可以看出来, ==IoC容器具备以下优点:

    资源不由使⽤资源的双⽅管理,⽽由不使⽤资源的第三⽅管理,这可以带来很多好处。第⼀,资源集
    中管理,实现资源的可配置和易管理。第⼆,降低了使⽤资源双⽅的依赖程度,也就是我们说的耦合
    度。

    1. 资源集中管理: IoC容器会帮我们管理⼀些资源(对象等), 我们需要使⽤时, 只需要从IoC容器中去取
      就可以了
    2. 我们在创建实例的时候不需要了解其中的细节, 降低了使⽤资源双⽅的依赖程度, 也就是耦合度.

    🍀DI 概念说明

    说到 IoC 不得不提的⼀个词就是“DI”,DI 是 Dependency Injection 的缩写,翻译成中⽂是“依赖注⼊”的意思。

    所谓依赖注⼊,就是由 IoC 容器在运⾏期间,动态地将某种依赖关系注⼊到对象之中。所以,依赖注⼊(DI)和控制反转(IoC)是从不同的⻆度的描述的同⼀件事情,就是指通过引⼊ IoC 容器,利⽤依赖关系注⼊的⽅式,实现对象之间的解耦。

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

    ⭕总结

    感谢大家的阅读,希望得到大家的批评指正,和大家一起进步,与君共勉!

  • 相关阅读:
    799. 最长连续不重复(双指针)
    通用商城项目(下)
    HI3519DV500快速启动
    COM组件IDispatch操作
    【LeetCode热题100】【子串】最小覆盖子串
    中国传统美食网页HTML代码 学生网页课程设计期末作业下载 美食大学生网页设计制作成品下载 DW餐饮美食网页作业代码下载
    入门数据库Days6
    Redis 入门 - C#|.NET Core客户端库六种选择
    C++:类的默认成员函数------构造函数&&析构函数(超详细解析,小白一看就懂!)
    Java学数据结构(4)——PriorityQueue(优先队列)& 二叉堆(binary heap)
  • 原文地址:https://blog.csdn.net/m0_65941010/article/details/137747914