• 【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);
            }
        }
    }
    
    • 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

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

    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
    • 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

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

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

    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
    • 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
    • 51
    • 52

    在这里插入图片描述

    1.3.3 对比

    在这里插入图片描述

    1.4 理解IoC

    在这里插入图片描述

    1.5 DI

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

  • 相关阅读:
    人工智能第2版学习——知情搜索2
    python 获取本机ip
    第八章《Java高级语法》第6节:匿名类
    SpringBoot整合RabbitMQ图文过程以及RabbitTemplate常用API介绍
    电子电气架构——由NRC优先级引起的反思
    半个月看完这份Java八股文,我终于面进了阿里
    代码随想录第二天
    图片点击出现边框样式(一图出现边框,其他图取消边框)
    MATLAB | 对随机信号进行统计分析,绘制频次直方图、频率分布图,与理论概率密度进行比较
    重学Java8新特性(四) : 日期时间API、LocalDateTime、DateTimeFormatter、开发中时间工具类(常用)
  • 原文地址:https://blog.csdn.net/m0_68101404/article/details/134524103