Spring Ioc和DI

一、Spring是什么

Spring是一个轻量级的Java开发框架,核心是控制反转(Ioc)和面向切面编程(AOP)。简单点说,Spring是一个包含了众多工具方法的Ioc容器。

二、Ioc是什么

控制反转(Ioc),也就是说Spring是一个"控制反转的容器",本质是获得依赖对象的过程被反转了。在传统开发模式中需要自己通过new创建对象(控制权在开发者手里),而Ioc模pring式把创建对象的任务交给Spring容器,由Spring容器统一创建对象,管理对象依赖,开发者仅需声明依赖。

举例:

1、传统开发

造一辆车:先设计轮子,然后根据轮子设计底盘,然后根据地盘设计车身,最后根据车身设计整个汽车。就是汽车依赖车身,车身依赖底盘,底盘依赖轮子。

java 复制代码
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 int...");
        }
        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("Framewoek 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);
        }
    }
}

这种写法如果需求变了,比如需要加轮胎的颜色,改变底层轮胎代码后,整个调用链都要改,这种写法耦合性太高。

我们换一种思路,先设计汽车,根据汽车设计车身,根据车身设计底盘,再根据底盘设计轮胎。

2、Ioc开发

java 复制代码
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{
        public 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);
        }
    }
}

将原来需要自己创建的下级类该为传递的方式,即使下级类发生变化,当前类无需修改.

java 复制代码
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{
        public 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;
        private String color;
        public Tire(int size,String color){
            this.size = size;
            this.color = color;
            System.out.println("轮胎尺寸: "+size);
            System.out.println("轮胎颜色: "+color);
        }
    }
}

3、Ioc优势

在传统代码的对象创建顺序是:Car->Framework->Bottom->Tire

Ioc:Tire->Bottom->Framework->Car

通用程序的实现代码,类的创建顺序是反的,传统代码是Car控制并创建了Framework,依次往下,而改进之后的控制权发生的反转,不再是使用方对象创建并控制依赖对象了,而是把依赖对象注入当前对象,依赖对象的控制权不再由当前类控制。

这样即使依赖类发生任何变化,当前类不受影响,这就是典型的控制反转。

这部分代码是Ioc容器做的工作,

资源由不使用资源的第三方管理,优点:①资源集中管理,实现资源的可配置和易管理。②降低了使用双方的依赖程度。

三、DI是什么

DI是依赖注入

容器在运行期间,动态的位应用程序提供运行时所依赖资源,称之为依赖注入。上面的Ioc开发实例的代码是通过构造函数的方式把依赖对象注入到需要使用对象中。

相关推荐
谈笑也风生3 分钟前
经典算法题型之排序算法(三)
java·算法·排序算法
自己的九又四分之三站台20 分钟前
导入数据到OG GraphQL以及创建graph
java·后端·graphql
强子感冒了22 分钟前
Java学习笔记:String、StringBuilder与StringBuffer
java·开发语言·笔记·学习
程序员JerrySUN37 分钟前
OP-TEE + YOLOv8:从“加密权重”到“内存中解密并推理”的完整实战记录
android·java·开发语言·redis·yolo·架构
想要一只奶牛猫1 小时前
Spring Web MVC(三)
前端·spring·mvc
+VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vueOA工程项目管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
JasmineWr1 小时前
Spring事务解析
java·spring
qq_336313931 小时前
java基础-IO流(缓冲流)
java·开发语言
青岛少儿编程-王老师1 小时前
CCF编程能力等级认证GESP—C++2级—20251227
java·开发语言·c++
高山上有一只小老虎1 小时前
小红的推荐系统
java·算法