如何理解Spring?

简介: 如何理解Spring?

Spring 是包含了众多⼯具⽅法的 IoC 容器

那何为容器呢?容器是用来容纳某种东西的装置。比如:List/Map 是数据存储容器,Tomcat 是Web 容器等等。Spring 也是⼀个容器,是⼀个 IoC 容器。

那何为IoC 呢?IoC = Inversion of Control 翻译成中⽂是“控制反转”的意思,也就是说 Spring 是⼀个“控制反转”的容器。那控制反转具体是什么呢?下面代码举例:

构建⼀辆⻋(Car Class),然⽽⻋需要依赖⻋身(FrameWork Class),⽽⻋身需要依赖底盘(Bottom Class),⽽底盘需要依赖轮胎(Tire Class),最终程序的实现代码如下:

public class NewCarExample { 
   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); 
      } 
  } 
}

以上程序中,轮胎的尺⼨的固定的,然⽽随着对的⻋的需求量越来越⼤,个性化需求也会越来越多(比如轮胎大小,颜色,材质等等),这时候我们就需要修改轮胎类的代码,但是修改轮胎类代码就会影响到底盘类代码,修改底盘类代码就会影响到车身类代码,依次向上修改。这样的代码存在高度的耦合性。每一次调用链最底层发生改变,如果调用链很长,耦合性很高,那么整个调用链都需要修改代码。现在的需求就是修改任意类,不影响整个调用链,这种做法就叫做解耦

我们将控制权反转,此时,我们只需要将原来由⾃⼰创建的下级类,改为传递的⽅式(也就是注⼊的⽅式),因为我们不需要在当前类中创建下级类了,所以下级类即使发⽣变化(创建或减少参数),当前类本身也⽆需修改任何代码,这样就完成了程序的解耦。

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 { 
       private Framework framework; 
       public Car(Framework framework) { 
           this.framework = framework; 
      } 
       public void run() { 
           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); 
      } 
  } 
}

通过解耦,从⽽实现了更加灵活、通⽤的程序设计了。通⽤程序的实现代码,类的创建顺序是反的,传统代码是 Car 控制并创建了Framework,Framework 创建并创建了 Bottom,依次往下,⽽改进之后的控制权发⽣的反转,不再是上级对象创建并控制下级对象了,⽽是下级对象把注⼊到当前对象中,下级的控制权不再由上级类控制了,这样即使下级类发⽣任何改变,当前类都是不受影响的,这就是典型的控制反转,也就是 IoC 的实现思想。

既然Spring 是⼀个IoC容器,那么它有两个最核心的功能:把对象存入容器和从容器中取出对象。

当谈到IoC,必然会谈到DI,DI 是 Dependency Injection 的缩写,翻译成中⽂是“依赖注⼊”的意思。从广义上老说,IoC和DI其实说的是一回事,但是也有区别的。这样理解,IoC是一种思想,而DI就是这种思想的具体实现。举个例子:今天晚上要去吃一顿好的,这就是一个思想,那今天晚上去吃海底捞吧,吃海底捞这就是DI,一种具体实现。


总结:Spring 是包含了众多⼯具⽅法的 IoC 容器,它里面包含了两个最核心的功能:将对象存入容器,从容器将对象取出来,供其它类使用。跟Spring相关的两个概念,一个是IoC,一个是DI。IoC是一种思想,而DI是这种思想的一种具体实现。


相关文章
|
搜索推荐 Java 微服务
SpringCloud升级之路2020.0.x版-9.如何理解并定制一个Spring Cloud组件
SpringCloud升级之路2020.0.x版-9.如何理解并定制一个Spring Cloud组件
SpringCloud升级之路2020.0.x版-9.如何理解并定制一个Spring Cloud组件
|
1天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
56 0
|
1天前
|
缓存 Java Maven
Spring Boot自动配置原理
Spring Boot自动配置原理
52 0
|
1天前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
138 0
|
1天前
|
存储 JSON Java
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
46 2
|
1天前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
|
1天前
|
前端开发 Java 应用服务中间件
Springboot对MVC、tomcat扩展配置
Springboot对MVC、tomcat扩展配置
|
1天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
1天前
|
安全 Java 应用服务中间件
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置
33 0
江帅帅:Spring Boot 底层级探索系列 03 - 简单配置