Spring5.0原始碼學習系列之核心思想IOC概述(三)
提示:Spring官網對Spring的主要技術做了比較詳細的介紹,詳情參考Spring官網spring core檔案,IOC實作是springframework的一個核心功能,本博客對spring ioc進行比較大概的描述,僅作為學習原始碼之前的科普
文章目錄
- 前言
- 一、IOC是什么?
- 二、IOC容器的作用
- 三、IOC和DI區別
- 四、Spring容器概述
- 五、如何使用IOC容器?
- 5.1、配置元資料
- 5.2、實體IOC容器
- 5.3、使用IOC容器
- 六、Spring Bean概述
- 6.1、Spring中的Bean概述
- 6.2、實體Bean的方式
- 七、定義SpringBean
- 7.1、定義Spring Bean
- 7.2、Bean作用域
- 7.3、Bean懶加載模式
- 八、Spring生命周期
- 九、Spring后置處理器
- 十、什么是回圈依賴?
- 歸納
前言
提示:Spring框架原始碼是java領域比較好的原始碼,原因是這款框架架構設計很好,框架拓展性靈活,這就是spring框架流行的原因之一,而Ioc(控制反轉)模塊和aop是springframework的兩個核心功能,ioc和aop是一種技術思想,早在springframework框架開發之前就已經提出來了,不過更偏向于理論,而spring在技術層面對這兩個思想做了很好的實作(java領域)
提示:以下是本篇文章正文內容,下面案例可供參考,僅作為學習ioc實作原始碼之前的科普,因為ioc原始碼比較復雜,學習原始碼之前要一些必要的了解,首先應用都不熟悉,看原始碼肯定是看不懂的,所以要先通過學習了解熟悉spring的ioc實作
一、IOC是什么?
IOC:Inversion of controll(控制反轉 / 反轉控制),ioc是一種技術思想,在spring框架開發之前就已經提出來的,spring框架對ioc技術思想進行了很好的實作
對控制反轉的理解:控制反轉(IOC)可以理解為獲取依賴物件的方式反轉過來,有反轉就有“正轉”,所謂“正轉”可以這樣理解,“正轉”就是常規的獲取物件方式,比如類A依賴于類B,在類A中要獲取類B,就是new一個B物件既可,這種就是“正轉“,這種方法對依賴物件的管理獲取,或者說是由類A去觸發的,而反轉的情況是對依賴物件的控制都交給spring ioc容器,物件的創建管理都給ioc容器,需要什么物件,直接通過ioc容器拿既可
不依賴于IOC的情況:直接通過new獲取依賴物件

有Spring IOC容器的情況:所有物件的創建管理都給spring ioc容器控制,需要什么物件直接找ioc容器拿,如圖:

為什么叫控制反轉?
- 控制:指的是對依賴物件控制,實體,管理
- 反轉:對依賴物件的控制交給外部環境,也即Spring IOC容器
二、IOC容器的作用
參考其它博客對ioc解的詮釋:http://www.52codes.net/article/40150.html,從圖可以看出ioc容器其實就是用于解耦的,體現了oop思想

ps:沒有IoC的程式中我們使用面向物件編程物件的創建與物件間的依賴關系完全硬編碼在程式中
三、IOC和DI區別
DI:Dependancy Injection(依賴注入),控制反轉(IOC)和依賴注入(DI)其實描述的是同一件事,只不過是角度不同
- 控制反轉(IOC)站在物件角度,強調物件的控制交給(反轉給)IOC容器,IOC容器控制物件的實體、管理
- 依賴注入(DI)站在IOC容器的角度,說明的是IOC容器將依賴物件注入到IOC容器,假如類A宣告了類B的屬性,在ioc容器啟動時候就需要將類B物件注入給類A
四、Spring容器概述
去Spring官網找到IOC容器的相關說明檔案,發現官方檔案還是比較詳細的,雖然沒有對Spring框架原始碼進行分析,不過也對IOC進行了很詳細的說明,詳情可以參考官方檔案,本博客參考官方檔案進行摘要說明:

Spring官網對Spring ioc容器的描述:
This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) [1] principle. IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.
其實意思也和前面說明的相近,也是說明控制反轉(IOC)其實就是將依賴物件的控制反轉給IOC容器
官網的圖片,描述了spring容器管理物件的大概程序:

五、如何使用IOC容器?
也即官方檔案說明的三個步驟:

翻譯過來就是:
- 配置元資料
- 實體IOC容器
- 使用IOC容器
ok,跟著官方檔案,進行學習:
5.1、配置元資料
官網介紹了兩種方式:也即基于注解的,另外一種是基于java配置的

基于注解的,主要是spring官方的提供的主鍵,比如經常用的@Autowired主鍵等等,詳情可以參考官方檔案

基于java配置的有兩種方法,一種是寫一個@Configuration配置類,一個是通過xml組態檔,詳情可以參考官方檔案:

@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
用思維導圖,簡單畫圖:

5.2、實體IOC容器
實體一個Spring IOC容器,官網的例子是:
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- services -->
<bean id="petStore" class="org.springframework.samples.jpetstore.services.PetStoreServiceImpl">
<property name="accountDao" ref="accountDao"/>
<property name="itemDao" ref="itemDao"/>
<!-- additional collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions for services go here -->
</beans>
簡單做下歸納:

5.3、使用IOC容器
實體好IOC容器之后,既可獲取對應的依賴物件:

六、Spring Bean概述
6.1、Spring中的Bean概述
Bean概述:Spring中的Bean指Beandefinition,Beandefinition是經過Spring容器實體的一個實體
6.2、實體Bean的方式


官網代碼示例:靜態工廠實體
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
工廠實體:
public class DefaultServiceLocator {
private static ClientService clientService = new ClientServiceImpl();
public ClientService createClientServiceInstance() {
return clientService;
}
}
七、定義SpringBean
7.1、定義Spring Bean
Spring官網給出了如何定義一個Spring Bean:

本博客用思維導圖做下歸納:

7.2、Bean作用域

| 范圍 | 描述 |
|---|---|
| 單例(Singleton) | (默認)單例的beanDefinition |
| 原型(prototype) | 多例,多個實體的beanDefinition |
| 請求(request) | 單例的, 作用于HTTP 請求的生命周期 |
| 會話(session) | 單例的, 作用Http會話的宣告周期 |
| 應用(application) | 單例的,應用于ServletContext生命周期 |
| 網路socket(webSocket) | 單例的,應用于Websocket環境 |
單例模式的圖例,圖來自Spring官網

原型模式,也稱之為多例模式,圖例來自Spring官網:

7.3、Bean懶加載模式
懶加載(Lazy-initialized beans):懶加載模式是bean在第一次呼叫時候被實體,而不是spring容器啟動時候,默認是不開啟的,( A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.),通過改配置lazy-init=“true”
八、Spring生命周期
Spring的生命周期是Spring框架的一個比較重要的方面,不過內容比較多,所以本博客只是簡單進行描述

九、Spring后置處理器
后置處理器是Spring框架的一個比較重要的方面,不過內容比較多,所以本博客只是簡單進行描述

十、什么是回圈依賴?
兩個及以上的類互相呼叫依賴,形成倍訓,Spring框架檢測到這種場景會拋 BeanCurrentlyInCreationException,提前暴露物件的方法


歸納
提示:這里對文章進行總結:
例如:以上就是今天要講的內容,本文僅僅簡單介紹了pandas的使用,而pandas提供了大量能使我們快速便捷地處理資料的函式和方法,

CSDN認證博客專家
分布式
Java
Spring
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/195784.html
標籤:其他
