Spring?加載?Application?Context五種方式小結(jié)
容器是Spring框架的核心。Spring 容器使用DI管理構(gòu)成應(yīng)用的組件。Spring容器使用DI管理構(gòu)成應(yīng)用的組件。這些對象更簡單、易于理解,更易于重用并且更易于進(jìn)行單元測試。
Spring 容器
Spring 容器并不是只有一個。Spring 自帶了多個容器實(shí)現(xiàn),可以歸為兩種不同的類型。
Bean工廠(由org.springframework,beansfactory.BeanFactory 接口定義)是最簡單的容器,提供DI的支持。
應(yīng)用上下文(由org.springframework.context.ApplicationContext 接口定義)基于BeanFactory構(gòu)建,并提供應(yīng)用框架級別的服務(wù)。(例如 從屬性文件解析文本信息以及發(fā)布應(yīng)用事件給感興趣的事件監(jiān)聽者。)
Spring多種類型應(yīng)用上下文
Spring自帶了多種類型的應(yīng)用上下文。下面羅列的幾個大家一定用過。
AnnotationConfigApplicationContext:從一個或多個 基于Java的配置類中加載Spring應(yīng)用上下文。AnnotationConfigWebApplicationContext:從一個或 多個基于Java的配置類中加載Spring Web應(yīng)用上下ClassPathXmlApplicationContext:從類路徑下的一個或 多個XML配置文件中加載上下文定義,把應(yīng)用上下文的定義文件作為類資源。FileSystemXmlapplicationcontext:從文件系統(tǒng)下的一 個或多個XML配置文件中加載上下文定義。XmlWebApplicationContext:從Web應(yīng)用下的一個或多個 XML配置文件中加載上下文定義。
FileSystemXmlapplicationcontext 的例子
// FileSystemXmlApplicationContext() 存放的是具體文件路徑
ApplicationContext context = new FileSystemXmlApplicationContext("D:/work_spring/application.xml");String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"}; ? ? ? ? ?
ApplicationContext ctx = new FileSystemXmlApplicationContext(locations );ClassPathXmlApplicationContext 的例子
ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");String[] locations = {"bean1.xml", "bean2.xml", "bean3.xml"};
ApplicationContext ctx = new ClassPathXmlApplicationContext(locations);使用
FileSystemXmlApplicationContext和使 用ClassPathXmlApplicationContext的區(qū)別在于:FileSystemXmlApplicationContext在指定的文件系統(tǒng)路徑下查找application.xml 的文件;ClassPathXmlApplicationContext是在所有的類路徑(包含JAR文件)項(xiàng)目的resource目錄下查找 application.xml文件。
AnnotationConfigApplicationContext
使用AnnotationConfigApplicationContext可以實(shí)現(xiàn)基于Java的配置類加載Spring的應(yīng)用上下文。避免使用application.xml進(jìn)行配置。相比XML配置,更加便捷。請參考下面的例子。
1、Entitlement 實(shí)體類
public class Entitlement {
private String name;
private int time;
// 省略set get方法
}2、配置類
@Configuration
public class AppConfig {
@Bean(name = "entitlement")
public Entitlement entitlement() {
Entitlement ent = new Entitlement();
ent.setName("Entitlement");
ent.setTime(1);
return ent;
}
@Bean(name = "entitlement2")
public Entitlement entitlement2() {
Entitlement ent = new Entitlement();
ent.setName("Entitlement2");
ent.setTime(2);
return ent;
}
}3、測試類
public static void main(String[] arg) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class);
ctx.refresh();
Entitlement ent = (Entitlement)ctx.getBean("entitlement");
System.out.println(ent.getName());
System.out.println(ent.getTime());
Entitlement ent2 = (Entitlement)ctx.getBean("entitlement2");
System.out.println(ent2.getName());
System.out.println(ent2.getTime());
ctx.close();
}4、測試結(jié)果
Entitlement
1
Entitlement2
2
AnnotationConfigWebApplicationContext
在Spring MVC中使用基于Java的配置,我們需要告訴 DispatcherServlet和ContextLoaderListener使 用AnnotationConfigWebApplicationContext,這是一 個WebApplicationContext的實(shí)現(xiàn)類,它會加載Java配置類,而 不是使用XML。要實(shí)現(xiàn)這種配置,我們可以設(shè)置contextClass上下文參 數(shù)以及DispatcherServlet的初始化參數(shù)。如下的程序清單展現(xiàn) 了一個新的web.xml,在這個文件中,它所搭建的Spring MVC使用基 于Java的Spring配置:
設(shè)置web.xml使用基于Java的配置

XmlWebApplicationContext
ContextLoader初始化Spring Web上下文的determineContextClass方法中,我們知道Spring首先通過Servlet上下文從web.xml文件中獲取用戶自定義配置的contextClass參數(shù)值,如果沒有獲取到,則默認(rèn)使用Spring的XmlWebApplicationContext作為Spring Web應(yīng)用的IoC容器,XmlWebApplicationContext是WebApplicationContext的實(shí)現(xiàn)類ConfigurableWebApplicationContext的子類。
Spring創(chuàng)建出現(xiàn)的錯誤,ApplicationContext錯誤
在進(jìn)行創(chuàng)建spring容器的時候出現(xiàn)了這樣的錯誤提示:
Type mismatch: cannot convert from ClassPathXmlApplicationContext to ApplicationContext
(如下圖所示)

出現(xiàn)這樣出錯誤的原因在于我們在創(chuàng)建導(dǎo)入包的時候出現(xiàn)的錯誤:
import org.apache.catalina.core.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;
出現(xiàn)這種錯誤的很大原因在于我們導(dǎo)入了:import org.apache.catalina.core.ApplicationContext; 這個錯誤的包
解決方案
將其修改為:
import org.springframework.context.ApplicationContext;
錯誤解決!
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC通過模型視圖ModelAndView渲染視圖的實(shí)現(xiàn)
這篇文章主要介紹了SpringMVC通過模型視圖ModelAndView渲染視圖的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
SpringBoot項(xiàng)目中使用Swagger2及注解解釋的詳細(xì)教程
Swagger2是一個開源項(xiàng)目,用于為RESTful Web服務(wù)生成REST API文檔,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目中使用Swagger2及注解解釋的詳細(xì)教程,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
MyBatis 接收數(shù)據(jù)庫中沒有的字段的解決
這篇文章主要介紹了MyBatis 接收數(shù)據(jù)庫中沒有的字段的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java數(shù)據(jù)庫連接池連接Oracle過程詳解
這篇文章主要介紹了Java數(shù)據(jù)庫連接池連接Oracle過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09
Java實(shí)現(xiàn)支付寶之第三方支付寶即時到賬支付功能
這篇文章主要介紹了Java實(shí)現(xiàn)支付寶之第三方支付寶即時到賬支付功能的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07
詳解使用spring aop實(shí)現(xiàn)業(yè)務(wù)層mysql 讀寫分離
本篇文章主要介紹了使用spring aop實(shí)現(xiàn)業(yè)務(wù)層mysql 讀寫分離,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01

