认识 spring 框架基本概念


前言

本篇简单介绍了spring是什么,认识IoC容器, 了解spring的核心功能,认识DI; 如有错误,请在评论区指正,让我们一起交流,共同进步!



本文开始

1. 什么是spring?

spring: spring 是一个开源框架,一个包含众多方法工具的IoC容器;

1.1 认识什么是容器?

容器:用来存储东西的一种物品;
这里所说的spring也是一种容器,用来存储对象;

1.2 认识什么是IoC?

IoC: 英文Inversion of Control 控制反转;
所以说spring也是一种控制反转容器;

  • 什么是控制反转呢?
    传统的使用一个对象:通过new对象来创建,也就是控制权在程序员手中;=》主动创建
    spring(IoC)框架中使用一个对象: 所需要的对象,都由容器去创建,不需要去关注创建的细节,也就是如何创建的;获取也从容器中获取,也就是控制权在spring容器中;
    由上述所说,实现了控制反转; 对象的生命周期由程序员控制 转变到 由spring框架控制;

  • IoC容器的优点:解耦合:解决代码中的耦合性;
    传统方法使用,如果对象与对象之间代码相互依赖,如果更改最底层的代码,会引起上层代码逐个更改;为了解决这种问题,使用spring框架,如果底层更改,只是更改Ioc容器中的代码,我们不需要去关注;

1.3 spring 的核心功能

1)将对象存到容器中
2)从容器中取对象

1.4 认识DI

DI:英文Dependency Injection,依赖注入,指在程序运行期间,动态的将依赖对象取到的过程;

  • IOC 与 DI的区别?
    IOC:是一种设计思想;
    DI:是一种具体实现的技术;

示例:今天中午饭吃什么=》这是一种思想,吃米线,火锅等是一种具体的实现技术;

1.5 示例

通过示例明白,为什么使用IOC容器可以解耦合;
传统的创建一个对象:每个对象都需要new, 这里举例的是车,传统情况下对象之间都是相互依赖的;
【注】每个类一个java文件

//模拟传统开发:Car => Framework => Bottom => Tire
public class Car {
    private Framework framework;

    //有车必须现有车的框架, 通过构造方法实现
    public Car(int size) {
        this.framework = new Framework(size);
    }
    public void init() {
        //车实现的关键的代码
        System.out.println("Car init()");
        framework.init();
    }
}

public class Framework {
    private Bottom bottom;

   public Framework(int size) {
       this.bottom = new Bottom(size);
   }
    public void init() {
        System.out.println("Framework init()");
        bottom.init();
    }
}

public class Bottom {
    private Tire tire;

    public Bottom(int size) {
        this.tire = new Tire(size);
    }
    public void init() {
        System.out.println("Bottom init()");
        tire.init();
    }
}

public class Tire {
    //轮胎的大小
    private int size = 10;

    public Tire(int size){
        this.size = size;
    }

    public void init() {
        System.out.println("Tire init() Size= " + this.size);
    }
}

public class Test {
    public static void main(String[] args) {
        Car car = new Car(20);
        car.init();
    }
}

使用spring容器再次实现:对象都在容器中创建,不需要程序员自己new,一些修改操作不影响每层,不用像传统写法底层修改,上层依次修改;而修改操作是spring容器中实现的,不用程序员了解其中细节,直接使用即可;

//测试类,用来模拟IOC容器
public class Test {
    private Car car;
    private Framework framework;
    private Bottom bottom;
    private Tire tire;
    public Test() {
        this.car = new Car(this.framework);
        this.framework = new Framework(this.bottom);
        this.bottom = new Bottom(this.tire);
        this.tire = new Tire(20);
    }
    public static void main(String[] args) {
        Test test = new Test();
        test.car.init();
    }
}

public class Car {
    private Framework framework;
    public Car(Framework framework) {
        this.framework = framework;
    }
    public void init() {
        System.out.println("Car init()");
        framework.init();
    }
}

public class Framework {
    private Bottom bottom;
    public Framework(Bottom bottom) {
        this.bottom = bottom;
    }
    public void init() {
        System.out.println("Framework init()");
        bottom.init();
    }
}

public class Bottom {
    private Tire tire;
    public Bottom(Tire tire) {
        this.tire = tire;
    }
    public void init() {
        System.out.println("Bottom init()");
        tire.init();
    }
}

public class Tire {
    private int size;
    public Tire(int size) {
        this.size = size;
    }
    public void init() {
        System.out.println("Tire init() Size= " + this.size);
    }
}


总结

✨✨✨各位读友,本篇分享到内容如果对你有帮助给个👍赞鼓励一下吧!!
感谢每一位一起走到这的伙伴,我们可以一起交流进步!!!一起加油吧!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值