首頁(yè)技術(shù)文章正文

如何創(chuàng)建ApplicationContext接口實(shí)例?

更新時(shí)間:2020-12-17 來(lái)源:黑馬程序員 瀏覽量:

1577370495235_學(xué)IT就到黑馬程序員.gif

ApplicationContext是BeanFactory的子接口,也被稱為應(yīng)用上下文,是另一種常用的Spring核心容器。它由org.springframework.context. ApplicationContext接口定義,不僅包含了BeanFactory的所有功能,還添加了對(duì)國(guó)際化、資源訪問(wèn)、事件傳播等方面的支持。

創(chuàng)建ApplicationContext接口實(shí)例,通常采用兩種方法,具體如下:

1、通過(guò)ClassPathXmlApplicationContext創(chuàng)建

ClassPathXmlApplicationContext會(huì)從類路徑classPath中尋找指定的XML配置文件,找到并裝載完成ApplicationContext的實(shí)例化工作。其使用語(yǔ)法如下:

ApplicationContext applicationContext =
new ClassPathXmlApplicationContext(String configLocation);

上述代碼中,configLocation參數(shù)用于指定Spring配置文件的名稱和位置。如果其值為“applicationContext.xml”,則Spring會(huì)去類路徑中查找名稱為applicationContext.xml的配置文件。

2、通過(guò)FileSystemXmlApplicationContext創(chuàng)建

FileSystemXmlApplicationContext會(huì)從指定的文件系統(tǒng)路徑(絕對(duì)路徑)中尋找指定的XML配置文件,找到并裝載完成ApplicationContext的實(shí)例化工作。其使用語(yǔ)法如下:

ApplicationContext applicationContext =
new FileSystemXmlApplicationContext(String configLocation);

與ClassPathXmlApplicationContext有所不同的是,在讀取Spring的配置文件時(shí),F(xiàn)ileSystemXmlApplicationContext不再?gòu)念惵窂街凶x取配置文件,而是通過(guò)參數(shù)指定配置文件的位置,例如“D:/workspaces/applicationContext.xml”。如果在參數(shù)中寫的不是絕對(duì)路徑,那么方法調(diào)用的時(shí)候,會(huì)默認(rèn)用絕對(duì)路徑來(lái)找。這種采用絕對(duì)路徑的方式,會(huì)導(dǎo)致程序的靈活性變差,所以這個(gè)方法一般不推薦使用。

在使用Spring框架時(shí),可以通過(guò)實(shí)例化其中任何一個(gè)類來(lái)創(chuàng)建ApplicationContext容器。通常在Java項(xiàng)目中,會(huì)采用通過(guò)ClassPathXmlApplicationContext類來(lái)實(shí)例化ApplicationContext容器的方式,而在Web項(xiàng)目中,ApplicationContext容器的實(shí)例化工作會(huì)交由Web服務(wù)器來(lái)完成。Web服務(wù)器實(shí)例化ApplicationContext容器時(shí),通常會(huì)使用基于ContextLoaderListener實(shí)現(xiàn)的方式,此種方式只需要在web.xml中添加如下代碼:

<!--  指定Spring配置文件的位置,多個(gè)配置文件時(shí),以逗號(hào)分隔-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <!--  Spring將加載 spring目錄下的applicationContext.xml文件 -->
    <param-value>
         classpath:spring/applicationContext.xml
    </param-value>
</context-param>
<!--  指定以ContextLoaderListener方式啟動(dòng)Spring容器 -->
<listener>
    <listener-class>
         org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

在本書后面章節(jié)講解三大框架整合以及項(xiàng)目時(shí),將采用基于ContextLoaderListener的方式由Web服務(wù)器實(shí)例化ApplicationContext容器。

創(chuàng)建Spring容器后,就可以獲取Spring容器中的Bean。Spring獲取Bean的實(shí)例通常采用以下兩種方法:

● Object getBean(String name):根據(jù)容器中Bean的id或name來(lái)獲取指定的Bean,獲取之后需要進(jìn)行強(qiáng)制類型轉(zhuǎn)換。

● T getBean(Class requiredType):根據(jù)類的類型來(lái)獲取Bean的實(shí)例。由于此方法為泛型方法,因此在獲取Bean之后不需要進(jìn)行強(qiáng)制類型轉(zhuǎn)換。

小提示:

BeanFactory和ApplicationContext兩種容器都是通過(guò)XML配置文件加載Bean的。二者的主要區(qū)別在于,如果Bean的某一個(gè)屬性沒(méi)有注入,使用BeanFacotry加載后,在第一次調(diào)用getBean()方法時(shí)會(huì)拋出異常,而ApplicationContext則在初始化時(shí)自檢,這樣有利于檢查所依賴屬性是否注入。因此,在實(shí)際開發(fā)中,通常都優(yōu)先選擇使用ApplicationContext,而只有在系統(tǒng)資源較少時(shí),才考慮使用BeanFactory。


猜你喜歡:
Ribbon負(fù)載均衡算法父接口IRule接口介紹

InputFormat接口的定義代碼如何設(shè)置?

Java面向接口編程實(shí)現(xiàn)方法

Java高級(jí)軟件工程師培訓(xùn)培訓(xùn)課程



分享到:
在線咨詢 我要報(bào)名
和我們?cè)诰€交談!