??作者:銀河罐頭
??系列專欄:JavaEE
??“種一棵樹最好的時(shí)間是十年前,其次是現(xiàn)在”
Spring 是什么?
通常所說的 Spring 指的是 Spring Framework(Spring 框架)。
Spring 是包含多種工具方法的 IoC容器。
什么是 IoC?
IoC(Inversion of Control): 控制反轉(zhuǎn)
"控制反轉(zhuǎn)"又是什么意思?
下面以一個程序來舉例。
假如我們現(xiàn)在要寫一個構(gòu)建 Car 的程序。
實(shí)現(xiàn)思路是 : Car(車) -> Framework(框架) -> Bottom(底盤) -> Tire(輪胎) -> Size(輪胎尺寸)
傳統(tǒng)程序開發(fā)
// Car 類
public class Car {
private Framework framework;
public Car(){
framework = new Framework();
}
public void init(){
System.out.println("do car");
framework.init();
}
public static void main(String[] args) {
Car car = new Car();
car.init();
}
}
// Framework 類
public class Framework {
private Bottom bottom;
public Framework(){
bottom = new Bottom();
}
public void init(){
System.out.println("do framework");
bottom.init();
}
}
// Bottom 類
public class Bottom {
private Tire tire;
public Bottom(){
tire = new Tire();
}
public void init(){
System.out.println("do bottom");
tire.init();
}
}
// Tire 類
public class Tire {
private int size = 17;
public void init(){
System.out.println("do tire");
System.out.println("size = " + size);
}
}
可以發(fā)現(xiàn)上述代碼中 size 是寫死的,如果 我想size = 另一個值,就需要改動上述代碼。
// Car 類
public class Car {
private Framework framework;
public Car(int size){
framework = new Framework(size);
}
public void init(){
System.out.println("do car");
framework.init();
}
public static void main(String[] args) {
Car car = new Car(40);
car.init();
}
}
// Framework 類
public class Framework {
private Bottom bottom;
public Framework(int size){
bottom = new Bottom(size);
}
public void init(){
System.out.println("do framework");
bottom.init();
}
}
// Bottom 類
public class Bottom {
private Tire tire;
public Bottom(int size){
tire = new Tire(size);
}
public void init(){
System.out.println("do bottom");
tire.init();
}
}
// Tire 類
public class Tire {
private int size = 17;
public Tire(int size){
this.size = size;
}
public void init(){
System.out.println("do tire");
System.out.println("size = " + size);
}
}
可以看出,當(dāng)最底層的代碼改了之后,從下到上整個調(diào)用鏈的代碼都要改動,這些類彼此之間的耦合程度是非常高的。
- 如何解決上述問題?
目前是在每個類中自己創(chuàng)建下級類,如果對下級類的參數(shù)進(jìn)行修改,那么整個類也得跟著改。那我現(xiàn)在就不在這個類里面創(chuàng)建下級類了,我交給別人去創(chuàng)建,需要時(shí)傳遞(注入)就可以了。這樣就完成了程序的"解耦"。
控制反轉(zhuǎn)式程序開發(fā)
// Car 類
public class Car {
private Framework framework;
public Car(Framework framework){
this.framework = framework;
}
public void init(){
System.out.println("do car");
framework.init();
}
}
// Framework 類
public class Framework {
private Bottom bottom;
public Framework(Bottom bottom){
this.bottom = bottom;
}
public void init(){
System.out.println("do framework");
bottom.init();
}
}
// Bottom 類
public class Bottom {
private Tire tire;
public Bottom(Tire tire){
this.tire = tire;
}
public void init(){
System.out.println("do bottom");
tire.init();
}
}
// Tire 類
public class Tire {
private int size = 16;
private String color = "紅色";
public Tire(int size,String color){
this.size = size;
this.color = color;
}
public void init(){
System.out.println("size = " + size + " | color = " + color);
}
}
// Test 類
public class Test {
public static void main(String[] args) {
Tire tire = new Tire(30,"黑色");
Bottom bottom = new Bottom(tire);
Framework framework = new Framework(bottom);
Car car = new Car(framework);
car.init();
}
}
對比總結(jié)規(guī)律:
傳統(tǒng)代碼對象創(chuàng)建順序:Car -> Framework -> Bottom -> Tire
控制反轉(zhuǎn)對象創(chuàng)建順序:Tire -> Bottom -> Framework -> Car
回到最開始說的 Spring 的定義上:Spring 是包含多種工具方法的 IoC容器。
Spring ,作為一個 “容器”,應(yīng)該具備 2 個功能 :
1.從容器中取出對象
2.把對象存入容器中。
也就是說,Spring 最核心的功能,就是從 Spring 中獲取對象(Bean)和將對象存到 Spring 中。
要用這個對象,就從 Spring 中取就行了,不用了就把這個對象放回到 Spring 中。有點(diǎn)類似于"線程池"。
也就是把創(chuàng)建和銷毀對象的權(quán)力交給 Spring 了。
什么是DI?
DI = Dependency Injection (依賴注入)
就是在運(yùn)行 IoC 容器期間,動態(tài)的將某種依賴關(guān)系注入到對象中。
IoC 和 DI 是從不同角度,描述同一件事。引入 IoC 容器,利用 DI,最終實(shí)現(xiàn)解耦.文章來源:http://www.zghlxwxcb.cn/news/detail-431290.html
不過,IoC 更偏向于是一種"思想",DI更偏向于是一種"具體實(shí)現(xiàn)"。文章來源地址http://www.zghlxwxcb.cn/news/detail-431290.html
到了這里,關(guān)于Spring 核心與設(shè)計(jì)思想的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!