一.什么是Spring💢
我们平时所说的Spring其实是Spring FrameWork(Spring框架) ,他是一个开源的框架,有十分活跃而庞大的社区,所以Spring才能长久不衰,其支持广泛的业务场景,可以让开发Java企业级的应用程序变得更加简单。
简单概括来说Spring是一个包含众多工具方法的IoC容器。、
那么什么是IoC...
二.什么是容器💢
容器大家都不陌生,其出现在我们生活中的各个方面,容器就是用来容纳某种物体的装置。
那么在编程的世界里又有哪些容器呢..
- List/Map:数据存储容器
- Tomcat:Web容器
三.什么是IoC💢
明白了容器的概念之后我们要知道Spring也是一个容器,Spring是IoC的容器,那么什么是IoC,
IoC(Inversion of Control)也就是“控制反转”,也就是说Spring是“控制反转”的容器。
接下来让我们来理解一下何为控制反转。
1.传统程序的开发
例如现在我们要造辆车。
车(Car)需要车身(FrameWork),车身需要底盘(bottom),底盘需要轮胎(tire)
public class CarExample {
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 = 17;
public void init(){
System.out.println("轮胎尺寸 "+ size);
}
}
}
在很久以前人们对应汽车没那么多的特别需求,甚至觉得汽车就是黑色的,轮胎就是那么大的,但是如今,人们就汽车的需求越来越多样化,那么我们就需要对上面的程序进行修改,可是传统程序开发这种代码改了后需要对上方所有代码进行修改,如下。
public class CarExample {
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);
}
public void run(){
//依赖车身
frameWork.init();
}
}
static class FrameWork{
private Bottom bottom;
public FrameWork(int size){
bottom = new Bottom(size);
}
//依赖底盘
public void init(){
bottom.init();
}
}
static class Bottom{
private Tire tire;
public Bottom(int size){
tire = new Tire(size);
}
//依赖轮胎
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);
}
}
}
当我们修改了最底层代码后,麻烦的是我们需要修改上方所有与此相关的代码
我们可以将由原来自己创建的下级类改为传递的方式(也就是“注入”),这样就完成了程序代码的解耦。
就比如我们打造一辆汽车,本来是所有零配件都是自己造,也就是底盘,车身,轮胎都是一家工厂的流水线工作,那么这个时候所有零配件都固定死了,如果我们将其全部都外包出去,就是客户在我们这边下了具体零件的请求,我们根据此请求转给对应的外包工厂让他们来造对应的零件。
2.控制反转程序开发
那么就按照上方的说法我们将创建子类的方式改为注入。
public class NewCarExample {
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);
}
}
}
经过上面的调整,无论底层怎么变我们都不需要调整代码,实现了更灵活,更通用的程序设计。
Tire->Bottom->FrameWork->Car.(通过注入)
经过改进之后我们发现控制权发生了反转,不再是上级对象要用时创建下级对象,而是由下级对象注入上级,这就是金典的控制反转,也就是IoC的实现思想。
3.理解Spring IoC
Spring:包含众多工具方法的IoC容器
包含众多工具方法这工具方法我们之后再细说。
那么怎么理解Spring是一个IoC容器
我们明白了控制反转后那么Spring就是一个控制反转的容器,既然是一个容器那么就有两个功能
- 将对象存入容器
- 从容器中取出对象
也就是说学Spring最核心的功能,就是学如何将对象存入Spring中,再从Spring中获取对象的Spring 是⼀个 IoC 容器,说的是对象的创建和销毁的权利都交给 Spring来管理,但他本身又具备了存储对象和获取对象的能力。
4.什么是DI💢
既然说到了IoC,那么就不得不说另一个词“DI” Dependency Injection(依赖注入)
所谓依赖注入就是在IoC容器运行期间动态的将某种依赖关系注入到对象中,所以IoC和DI是从不同角度描述一件事,就是通过引入IoC容器,利用依赖关系注入的方式,实现对象之间的解耦。