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开发实例的代码是通过构造函数的方式把依赖对象注入到需要使用对象中。

相关推荐
无敌最俊朗@2 小时前
STL-vector面试剖析(面试复习4)
java·面试·职场和发展
追逐时光者2 小时前
一款开源、现代化的 WinForm UI 控件库
后端·.net
PPPPickup2 小时前
easychat项目复盘---获取联系人列表,联系人详细,删除拉黑联系人
java·前端·javascript
LiamTuc2 小时前
Java构造函数
java·开发语言
长安er2 小时前
LeetCode 206/92/25 链表翻转问题-“盒子-标签-纸条模型”
java·数据结构·算法·leetcode·链表·链表翻转
菜鸟plus+2 小时前
N+1查询
java·服务器·数据库
我要添砖java3 小时前
《JAVAEE》网络编程-什么是网络?
java·网络·java-ee
CoderYanger3 小时前
动态规划算法-01背包问题:50.分割等和子集
java·算法·leetcode·动态规划·1024程序员节
花月C3 小时前
个性化推荐:基于用户的协同过滤算法
开发语言·后端·算法·近邻算法
cci4 小时前
还在用conda?,试试uv,提高包的安装速度
后端