spring framework之IoC

spring官方文檔:版本Version 5.3.15
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans

image.png

ioc容器的基礎(chǔ)包

org.springframework.beans
org.springframework.context

ioc特點(diǎn)

1、更容易與Spring的AOP功能集成
2、消息資源處理(用于國際化)
3、事件發(fā)布
4、特定于應(yīng)用層的上下文,XXApplicationContext

BenFactory

BeanFactory是容器的基礎(chǔ)工廠接口,它有以下很多擴(kuò)展的XXFactory工廠和XXApplicationContext
image.png

ApplicationContext

它的包路徑

org.springframework.context.ApplicationContext
ApplicationContext是BeanFactory的完整超集,在本章中專門用于描述Spring的IoC容器,
你也可以簡單理解Spring IoC容器就是ApplicationContext。
ApplicationContext接口代表Spring IoC容器,負(fù)責(zé)實(shí)例化、配置和組裝bean。
容器通過讀取配置元數(shù)據(jù)來獲取關(guān)于實(shí)例化、配置和組裝哪些對(duì)象的指令。
配置元數(shù)據(jù)以XML、Java注釋或Java代碼,它有很多實(shí)現(xiàn)拓展,我們這里統(tǒng)稱為XXApplicationContext。
image.png

問題來了,容器有了,怎么往容器里加料(bean等資源)呢?

基于容器的配置
兩種方式,第一種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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <context:annotation-config/>

</beans>

說明:
這個(gè)配置 <context:annotation-config/> 隱式地向容器中注冊(cè)了一下幾種XXProcessor

Resource(資源)

如何從系統(tǒng)中加載資源進(jìn)入ioc容器?spring framework提供了一個(gè)Resource接口,其中有很多方法。

public interface Resource extends InputStreamSource {

    boolean exists();

    boolean isReadable();

    boolean isOpen();

    boolean isFile();

    URL getURL() throws IOException;

    URI getURI() throws IOException;

    File getFile() throws IOException;

    ReadableByteChannel readableChannel() throws IOException;

    long contentLength() throws IOException;

    long lastModified() throws IOException;

    Resource createRelative(String relativePath) throws IOException;

    String getFilename();

    String getDescription();
}

同時(shí)spring里有幾個(gè)內(nèi)置的接口實(shí)現(xiàn)

  • UrlResource
    使用場景:網(wǎng)URL和可用于訪問通常可通過URL訪問的任何對(duì)象,例如文件、HTTPS目標(biāo)、FTP目標(biāo)等

  • ClassPathResource
    此類表示應(yīng)從類路徑獲取的資源。它使用線程上下文類加載器、給定類加載器或給定類來加載資源。

  • FileSystemResource
    支持Path同時(shí)支持URL,即同時(shí)支持java.io.File和java.nio.file.Path處理器。

  • PathResource
    這是一個(gè)基于java.nio.file.Path處理器的實(shí)現(xiàn),所有的操作和處理都是基于Path API。

  • ServletContextResource
    這是ServletContext資源的資源實(shí)現(xiàn),它解釋相關(guān)web應(yīng)用程序根目錄中的相對(duì)路徑。

  • InputStreamResource
    InputStreamResource是給定InputStream的資源實(shí)現(xiàn)。只有在沒有具體的資源實(shí)現(xiàn)適用的情況下才應(yīng)該使用它。特別是,在可能的情況下,首選ByteArrayResource或任何基于文件的資源實(shí)現(xiàn)。

與其他資源實(shí)現(xiàn)不同,這是一個(gè)已打開資源的描述符。因此,它從isOpen()返回true。如果需要將資源描述符保留在某個(gè)位置,或者需要多次讀取流,請(qǐng)不要使用它。

ResourceLoader接口

public interface ResourceLoader {

    Resource getResource(String location);

    ClassLoader getClassLoader();
}
Resource template = ctx.getResource("some/resource/path/myTemplate.txt");

針對(duì)ClassPathXmlApplicationContext,該代碼返回ClassPathResource。如果對(duì)FileSystemXmlApplicationContext實(shí)例運(yùn)行相同的方法,它將返回一個(gè)FileSystemResource。對(duì)于WebApplicationContext,它將返回ServletContextResource。它同樣會(huì)為每個(gè)上下文返回適當(dāng)?shù)膶?duì)象。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容