• Java EE -- Spring


    Spring介绍

    1.什么是Spring

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

    什么是容器?

    List、Map、Set是存储数据的容器
    Tomcat 是Web容器
    Spring 是IoC容器

    2.IoC是什么

    IoC(Inversion of Control) ,译为控制反转。它不是什么技术,而是一种思想。在Java开发中,IoC意味着你将设计好的依赖对象B交给容器控制,而不是按照传统的方式,在你需要使用依赖对象 B 的对象 A 内部直接控制。

    传统代码的开发:

    /**
     * 造一个车
     */
    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);
            }
        }
    }
    
    • 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

    如果要修改车的尺寸:

    /**
     * 造一个车
     */
    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
    • 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
    • 60
    • 61

    由以上代码可以看到,当底层的类被修改时,那么整个调用链上的所有的类都需要改变。代码的耦合性太高。

    解决办法:

    /**
     * 造一辆车
     */
    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);
            }
        }
    }
    
    • 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
    • 60
    • 61
    • 62
    • 63
    • 64

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

    3.DI是什么

    DI(Dependency Injection),译为依赖注入。

    依赖注入:就是IoC容器在运行时,动态的将某种依赖关系注入到对象中。

    在这里插入图片描述

    IoC是一种思想,DI就是IoC的具体实现

    4.Spring的核心功能

    Spring的本质上是一个容器,容器才是Spring的核心,它具备容器的两个最基础的功能:
    (1)将对象存入到容器
    (2)从容器中取出对象
    也就是说,Spring最核心的功能就是将对象存入到Spring中,再从Spring中获取到对象。
    又因为Spring是一个IoC容器,那么它还具备管理对象的创建和销毁的权利。

  • 相关阅读:
    深度学习目标检测YOLO系列(v1-v3+tinyv3)
    python读取amazon s3上的文件到内存
    HCIA-实验命令基础学习:
    CSDN流量卷领取和使用保姆级教程——流量卷,恭喜获得每日任务奖励【1500曝光】可获得新增曝光,阅读转化,点赞转化,新增关注-流量卷,流量卷,流量卷
    SAP UI5 里的 Busy Indicator 控件使用概述
    JavaSE反射前置之Properties类
    海康研究院出品:具有场景自适应概念学习的无监督目标检测(附论文下载)...
    linux怎么修改只读权限
    解决WPF项目xaml出现“正在等待IntelliSense完成”的问题
    考虑储能电池参与一次调频技术经济模型的容量配置方法(matlab代码)
  • 原文地址:https://blog.csdn.net/qq_52025208/article/details/126217642