Spring零基礎(chǔ)到進階之鴻蒙開篇
Spring是什么
用一句簡單的話來概括Spring:Spring是包含了眾多工具方法的IoC容器。那么問題來了,什么是容器,什么是IOC容器?下面就一起來看看吧
1.什么是容器?
容器就是用來榮南某種物品的裝置,前面我們也是學(xué)了很多容器的,類似于List/Map就是一個存儲數(shù)據(jù)的容器,Tomcat就是一個存儲Web項目的容器。
2.什么是IOC?
IOC(inversion of Control)翻譯成中文就是“控制反轉(zhuǎn)”的意思,這具體是什么意思呢?通過一個例子來解釋一下:
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); } } }
此時就構(gòu)建出來一輛“汽車”,此時認(rèn)為只要打印出來了輪胎的尺寸就表示構(gòu)建成功了:
但是如果需求如果發(fā)生了變化,不再自己給定汽車的輪胎尺寸,而是根據(jù)用戶的需求來進行變化,此時就會發(fā)現(xiàn)要改的代碼不僅僅只是一個參數(shù)
public class NewCarUpdateExample { public static void main(String[] args) { //用戶自己給定尺寸 Car car = new Car(50); 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); } } }
這才僅僅是改變了一個輪胎尺寸,如果需求再需要加顏色、花紋、logo呢?那就會更加麻煩,這些都是當(dāng)?shù)讓影l(fā)生改變的時候,要修改的是整個調(diào)?鏈上的所有代碼,那么為什么會出現(xiàn)這樣的問題呢?
分析可知,這些類都是互相依賴的,耦合性非常強,那么該如何解耦呢?IoC就可以解決這樣的問題,將控制權(quán)反轉(zhuǎn)出去,不再自己掌控,而是將控制權(quán)交給IoC管理,只有自己使用的使用才調(diào)用
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); } } }
此時就不在類里面進行控制了,而是只有在需要使用的時候,才去傳入而是不是自己控制
再需要改動的時候的話,就只需要改最底層的,而不是改變整個控制鏈!
因此使用IOC最大的優(yōu)點就是實現(xiàn)了代碼的解耦;對象(Bean)生命周期交給IOC框架來維護,就不需要再關(guān)注對象的創(chuàng)建了!
3.理解Spring IoC
通過上面的對IoC的認(rèn)識,就可以得知Spring IoC容器最核心的功能了:
- 將Bean(對象)存儲到Spring(容器)中;
- 將Bean(對象)從Spring(容器)中取出來;
4.了解DI
說到IoC就不得不提起DI(Dependency Injection)了,翻譯過來就是依賴注入的意思,那具體代表什么呢?所謂依賴注?,其實就是在 IoC 容器運?期間,動態(tài)地將某種依賴關(guān)系注?到對象之中,其實DI和IoC是從不同的?度的描述的同?件事情,而IoC其實是一種思想,而DI是具體的落地實現(xiàn),思想就指導(dǎo)了具體的落地實現(xiàn)!
IoC和DI的區(qū)別:IoC是一種思想,DI是一種實現(xiàn)(類似于樂觀鎖和CAS);
第一次介紹到這就結(jié)束了,主要還是先來認(rèn)識一下Spring到底是什么,后面我會再介紹其具體使用和方法的!
到此這篇關(guān)于Spring零基礎(chǔ)到進階之鴻蒙開篇的文章就介紹到這了,更多相關(guān)Spring 基礎(chǔ)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java線程池:獲取運行線程數(shù)并控制線程啟動速度的方法
下面小編就為大家?guī)硪黄猨ava線程池:獲取運行線程數(shù)并控制線程啟動速度的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05IDEA教程創(chuàng)建SpringBoot前后端分離項目示例圖解
在使用spring、mybatis等框架時,配置文件很復(fù)雜,有時復(fù)雜的讓人想放棄Java,使用C#。springboot出現(xiàn)這一切問題就都不是問題2021-10-10SpringBoot從0到1整合銀聯(lián)無跳轉(zhuǎn)支付功能附源碼
這篇文章主要介紹了SpringBoot從0到1整合銀聯(lián)無跳轉(zhuǎn)功能支付附源碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Spring學(xué)習(xí)筆記之RestTemplate使用小結(jié)
這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)筆記之RestTemplate使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08