• 初识Spring


    在这里插入图片描述

    目录

    ?? Spring是什么

    用一句简单的话来概括Spring:Spring是包含了众多工具方法的IoC容器。那么问题来了,什么是容器,什么是IOC容器?下面就一起来看看吧

    ?? 1.什么是容器?

    容器就是用来荣南某种物品的装置,前面我们也是学了很多容器的,类似于List/Map就是一个存储数据的容器,Tomcat就是一个存储Web项目的容器。

    ?? 2.什么是IOC?

    IOC(inversion of Control)翻译成中文就是“控制反转”的意思,这具体是什么意思呢?通过一个例子来解释一下:

    public class NewCarExample {
        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);
            }
        }
    }
    
    • 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

    此时就构建出来一辆“汽车”,此时认为只要打印出来了轮胎的尺寸就表示构建成功了:
    在这里插入图片描述
    但是如果需求如果发生了变化,不再自己给定汽车的轮胎尺寸,而是根据用户的需求来进行变化,此时就会发现要改的代码不仅仅只是一个参数

    public class NewCarUpdateExample {
        public static void main(String[] args) {
            //用户自己给定尺寸
            Car car = new Car(50);
            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
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59

    在这里插入图片描述

    这才仅仅是改变了一个轮胎尺寸,如果需求再需要加颜色、花纹、logo呢?那就会更加麻烦,这些都是当底层发生改变的时候,要修改的是整个调链上的所有代码,那么为什么会出现这样的问题呢?
    在这里插入图片描述
    分析可知,这些类都是互相依赖的,耦合性非常强,那么该如何解耦呢?IoC就可以解决这样的问题,将控制权反转出去,不再自己掌控,而是将控制权交给IoC管理,只有自己使用的使用才调用

    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;
            }
            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

    在这里插入图片描述
    在这里插入图片描述
    此时就不在类里面进行控制了,而是只有在需要使用的时候,才去传入而是不是自己控制
    在这里插入图片描述
    再需要改动的时候的话,就只需要改最底层的,而不是改变整个控制链!
    因此使用IOC最大的优点就是实现了代码的解耦;对象(Bean)生命周期交给IOC框架来维护,就不需要再关注对象的创建了!

    ?? 3.理解Spring IoC

    通过上面的对IoC的认识,就可以得知Spring IoC容器最核心的功能了:

    • 将Bean(对象)存储到Spring(容器)中;
    • 将Bean(对象)从Spring(容器)中取出来;

    ?? 4.了解DI

    说到IoC就不得不提起DI(Dependency Injection)了,翻译过来就是依赖注入的意思,那具体代表什么呢?所谓依赖注,其实就是在 IoC 容器运期间,动态地将某种依赖关系注到对象之中,其实DI和IoC是从不同的度的描述的同件事情,而IoC其实是一种思想,而DI是具体的落地实现,思想就指导了具体的落地实现!
    IoC和DI的区别:IoC是一种思想,DI是一种实现(类似于乐观锁和CAS);


    第一次介绍到这就结束了,主要还是先来认识一下Spring到底是什么,后面我会再介绍其具体使用和方法的!
    在这里插入图片描述

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    Dubbo(一):Dubbo 3.0
    大数据面试题:Flink延迟数据是怎么解决的
    【OpenCV 例程300篇】234. 特征提取之主成分分析(PCA)
    中国招标投标协会李小林一行到访北京筑龙座谈交流
    VS2019 错误 MSB8066 自定义生成已退出,代码为 3
    11.ROS编程学习:参数管理机制python实现
    [量化投资-学习笔记016]Python+TDengine从零开始搭建量化分析平台-日志输出
    【单片机基础】按键状态机实现短按、长按、双击、三击和N击
    4.9 多协议标记交换MPLS
    【4天快速入门Python数据挖掘之第2天】Numpy——高效的运算工具
  • 原文地址:https://blog.csdn.net/m0_54866636/article/details/126080980