spring 是包含了众多⼯具⽅法的 ioc 容器.
ioc
什么是ioc?
像在类上⾯添加 @restcontroller 和@controller 注解, 就是把这个对象交给spring管理, spring 框架启动时就会加载该类. 把对象交给spring管理, 就是ioc思想.
ioc:inversion of control (控制反转), 也就是说 spring 是⼀个"控制反转"的容器.
什么是控制反转呢? 也就是控制权反转. 什么的控制权发⽣了反转? 获得依赖对象的过程被反转了也就是说, 当需要某个对象时, 传统开发模式中需要⾃⼰通过 new 创建对象, 现在不需要再进⾏创建, 把创建对象的任务交给容器, 程序中只需要依赖注⼊ (dependency injection,di)就可以了.
这个容器称为:ioc容器. spring是⼀个ioc容器, 所以有时spring 也称为spring 容器.
引入
传统实现思路
我们的实现思路是这样的:
先设计轮⼦(tire),然后根据轮⼦的⼤⼩设计底盘(bottom),接着根据底盘设计⻋⾝(framework),最后根据⻋⾝设计好整个汽⻋(car)。这⾥就出现了⼀个"依赖"关系:汽⻋依赖⻋⾝,⻋⾝依赖底盘,底盘依赖轮⼦.

代码实现
public class main {
public static void main(string[] args) {
car car = new car(21);
car.run();
car car2 = new car(17);
car2.run();
}
}
//汽车
class car {
private framework framework;
public car(int size) {
framework = new framework(size);
system.out.println("framework init...");
}
public void run() {
system.out.println("car run...");
}
}
//车身
class framework {
private bottom bottom;
public framework(int size) {
bottom = new bottom(size);
system.out.println("bottom init....");
}
}
//底盘
class bottom {
private tire tire;
public bottom(int size) {
tire = new tire(size);
system.out.println("tire init...");
}
}
//轮胎
class tire {
private int size;
public tire(int size) {
system.out.println("tire size:"+size);
}
}上面这样的设计看起来没问题,但是可维护性却很低,因为需求可能会越来越多,比如增加轮胎颜色,修改后的代码如下:

我们可以看到,修改后的代码会报错,并且需要我们继续修改

完整代码如下:
public class main {
public static void main(string[] args) {
car car = new car(21,"aaa");
car.run();
car car2 = new car(17,"bbb");
car2.run();
}
}
//汽车
class car {
private framework framework;
public car(int size,string color) {
framework = new framework(size,color);
system.out.println("framework init...");
}
public void run() {
system.out.println("car run...");
}
}
//车身
class framework {
private bottom bottom;
public framework(int size,string color) {
bottom = new bottom(size,color);
system.out.println("bottom init....");
}
}
//底盘
class bottom {
private tire tire;
public bottom(int size,string color) {
tire = new tire(size,color);
system.out.println("tire init...");
}
}
//轮胎
class tire {
private int size;
private string color;
public tire(int size,string color) {
system.out.println("tire size:"+size);
}
}从以上代码可以看出,以上程序的问题是:当最底层代码改动之后,整个调⽤链上的所有代码都需要修改.
程序的耦合度⾮常⾼(修改⼀处代码, 影响其他处的代码修改)。
解决方案
我们尝试改变实现方式:轮⼦依赖底盘, 底盘依赖⻋⾝,⻋⾝依赖汽⻋。

基于以上思路,我们把调⽤汽⻋的程序⽰例改造⼀下,把创建⼦类的⽅式,改为注⼊传递的⽅式。
完整代码如下:
class main {
public static void main(string[] args) {
tire tire = new tire(17, "red");
bottom bottom = new bottom(tire);
framework framework = new framework(bottom);
car car = new car(framework);
car.run();
}
}
//汽车
class car {
private framework framework;
public car(framework framework) {
this.framework = framework;
system.out.println("framework init...");
}
public void run() {
system.out.println("car run...");
}
}
//车身
class framework {
private bottom bottom;
public framework(bottom bottom) {
this.bottom = bottom;
system.out.println("bottom init....");
}
}
//底盘
class bottom {
private tire tire;
public bottom(tire tire) {
this.tire=tire;
system.out.println("tire init...");
}
}
//轮胎
class tire {
private int size;
private string color;
public tire(int size, string color) {
system.out.println("tire size:"+size+",color:"+color);
}
}代码经过以上调整,⽆论底层类如何变化,整个调⽤链是不⽤做任何改变的,这样就完成了代码之间的解耦,从⽽实现了更加灵活、通⽤的程序设计了。
ioc的优势
在传统的代码中对象创建顺序是:car -> framework -> bottom -> tire
改进之后解耦的代码的对象创建顺序是:tire -> bottom -> framework -> car

我们发现了⼀个规律,通⽤程序的实现代码,类的创建顺序是反的,传统代码是 car 控制并创建了framework,framework 创建并创建了 bottom,依次往下,⽽改进之后的控制权发⽣的反转,不再是使⽤⽅对象创建并控制依赖对象了,⽽是把依赖对象注⼊将当前对象中,依赖对象的控制权不再由当前类控制了.
这样的话, 即使依赖类发⽣任何改变,当前类都是不受影响的,这就是典型的控制反转,也就是 ioc 的实现思想。
到这⾥, 我们⼤概就知道了什么是控制反转了, 那什么是控制反转容器呢, 也就是ioc容器。

这部分代码, 就是ioc容器做的⼯作.
从上⾯也可以看出来, ioc容器具备以下优点:
资源不由使⽤资源的双⽅管理,⽽由不使⽤资源的第三⽅管理,这可以带来很多好处。
第⼀,资源集中管理,实现资源的可配置和易管理。
第⼆,降低了使⽤资源双⽅的依赖程度,也就是我们说的耦合度。
1. 资源集中管理: ioc容器会帮我们管理⼀些资源(对象等), 我们需要使⽤时, 只需要从ioc容器中去取就可以了
2. 我们在创建实例的时候不需要了解其中的细节, 降低了使⽤资源双⽅的依赖程度, 也就是耦合度.
spring 就是⼀种ioc容器, 帮助我们来做了这些资源管理.
di
di: dependency injection(依赖注⼊)
容器在运⾏期间, 动态的为应⽤程序提供运⾏时所依赖的资源,称之为依赖注⼊。
程序运⾏时需要某个资源,此时容器就为其提供这个资源.
从这点来看, 依赖注⼊(di)和控制反转(ioc)是从不同的⻆度的描述的同⼀件事情,就是指通过引⼊ ioc 容器,利⽤依赖关系注⼊的⽅式,实现对象之间的解耦。
前面代码中, 是通过构造函数的⽅式, 把依赖对象注⼊到需要使⽤的对象中的。

ioc 是⼀种思想,也是"⽬标", ⽽思想只是⼀种指导原则,最终还是要有可⾏的落地⽅案,⽽ di 就属于具体的实现。所以也可以说, di 是ioc的⼀种实现.
到此这篇关于spring ioc和di深度解析的文章就介绍到这了,更多相关spring ioc和di内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论