spring官方文檔:版本Version 5.3.15
https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#beans
ioc容器的基礎(chǔ)包
org.springframework.beans
org.springframework.context
ioc特點(diǎn)
1穗泵、更容易與Spring的AOP功能集成
2计寇、消息資源處理(用于國(guó)際化)
3踪栋、事件發(fā)布
4怖糊、特定于應(yīng)用層的上下文谣殊,XXApplicationContext
BenFactory
BeanFactory是容器的基礎(chǔ)工廠接口,它有以下很多擴(kuò)展的XXFactory工廠和XXApplicationContext
ApplicationContext
它的包路徑
org.springframework.context.ApplicationContext
ApplicationContext是BeanFactory的完整超集硝桩,在本章中專門(mén)用于描述Spring的IoC容器笙僚,
你也可以簡(jiǎn)單理解Spring IoC容器就是ApplicationContext。
ApplicationContext接口代表Spring IoC容器碾局,負(fù)責(zé)實(shí)例化荆残、配置和組裝bean。
容器通過(guò)讀取配置元數(shù)據(jù)來(lái)獲取關(guān)于實(shí)例化净当、配置和組裝哪些對(duì)象的指令内斯。
配置元數(shù)據(jù)以XML、Java注釋或Java代碼像啼,它有很多實(shí)現(xiàn)拓展俘闯,我們這里統(tǒng)稱為XXApplicationContext。
問(wèn)題來(lái)了忽冻,容器有了真朗,怎么往容器里加料(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>
說(shuō)明:
這個(gè)配置 <context:annotation-config/>
隱式地向容器中注冊(cè)了一下幾種XXProcessor
蝗碎。
。
旗扑。
XXXProcessor
如果在DispatcherServlet的WebApplicationContext中加了這個(gè)<context:annotation-config/>
配置蹦骑,這意味著,它只檢查Controller中的帶有注解@Autowired的bean臀防,而不檢查服務(wù)脊串。
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
使用場(chǎng)景:網(wǎng)URL和可用于訪問(wèn)通常可通過(guò)URL訪問(wèn)的任何對(duì)象祟昭,例如文件缕坎、HTTPS目標(biāo)、FTP目標(biāo)等ClassPathResource
此類表示應(yīng)從類路徑獲取的資源篡悟。它使用線程上下文類加載器谜叹、給定類加載器或給定類來(lái)加載資源。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)抡锈。只有在沒(méi)有具體的資源實(shí)現(xiàn)適用的情況下才應(yīng)該使用它疾忍。特別是,在可能的情況下床三,首選ByteArrayResource或任何基于文件的資源實(shí)現(xiàn)一罩。
與其他資源實(shí)現(xiàn)不同,這是一個(gè)已打開(kāi)資源的描述符撇簿。因此聂渊,它從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ì)象。