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 分钟前
fail2ban 实战:从默认配置到生产可用
后端·算法·架构
憧憬成为java架构高手的小白3 分钟前
langchain4j手敲理解
java·langchain
一个有温度的技术博主29 分钟前
两级缓存一致性实战:Redis + 本地缓存,如何不让数据“分裂”?
redis·spring·缓存
步行cgn30 分钟前
Spring AOP 实战:安全日志
python·安全·spring
做个文艺程序员32 分钟前
MQ第04篇:ActiveMQ深度解析:JMS规范、Spring JMS整合与企业级异步通信实战
spring·消息队列·activemq·java-activemq·jms
10mAh44 分钟前
【Java】ArrayList 遍历删除为什么报错?三种写法与漏删复现
java·开发语言·windows
JavaGuide1 小时前
DeepSeek Harness 重磅更新,Codex、Claude Code 子智能体进官方插件列表了,还有 Git 工作树和思考翻译!
前端·后端
thefool1122661 小时前
多线程 4
java
Rain的Java大神之路1 小时前
[特殊字符]8年Java老兵转型AI Agent:90%的人挂在同一个坑,根本不用学Python!(万字实战复盘,建议收藏)
java·数据库·人工智能·后端·python·spring·ai编程
高频因子挖掘机2 小时前
A股分钟K线数据占内存:先缩小请求范围,还是换 DataFrame 工具?
后端·github·api