spring mvc 下 applicationContext 和webApplicationContext

來源:互聯網
上載者:User

標籤:

spring中的ApplicationContexts可以被限制在不同的範圍。在web架構中,每個DispatcherServlet有它自己的WebApplicationContext,它包含了DispatcherServlet配置所需要的bean。DispatcherServlet 使用的預設BeanFactory是XmlBeanFactory,並且DispatcherServlet在初始化時會在你的web應用的WEB-INF目錄下尋找[servlet-name]-servlet.xml檔案。DispatcherServlet使用的預設值可以使用servlet初始化參數修改,

 

WebApplicationContext僅僅是一個擁有web應用必要功能的普通ApplicationContext。它和一個標準的ApplicationContext的不同之處在於它能夠解析主題,並且它知道和那個servlet關聯(通過到ServletContext的串連)。WebApplicationContext被綁定在ServletContext上,當你需要的時候,可以使用RequestContextUtils找到WebApplicationContext。

Spring的DispatcherServlet有一組特殊的bean,用來處理請求和顯示相應的視圖。這些bean包含在Spring的架構裡,(可選擇)可以在WebApplicationContext中配置,配置方式就象配置其它bean的方式一樣。這些bean中的每一個都在下面被詳細描述。待一會兒,我們就會提到它們,但這裡僅僅是讓你知道它們的存在以便我們繼續討論DispatcherServlet。對大多數bean,都提供了預設值,所有你不必要擔心它們的值。

 

 

// 一下來源於網路

 

servletContext 是web應用程式的大環境,用於儲存整個web應用程式層級的對象,不知道這樣說法是否對.

ApplicationContext,WebApplicationContext 是Spring的BeanFactory,從名字中就可以知道區別拉,一個是支援web特性的BeanFactory。

Spring擷取WebApplicationContext與ApplicationContext的幾種方法:

方法一:在初始化時儲存ApplicationContext對象 
代碼: 
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); 
ac.getBean("beanId"); 
說明:這種方式適用於採用Spring架構的獨立應用程式,需要程式通過設定檔手工初始化Spring的情況。 

方法二:通過Spring提供的工具類擷取ApplicationContext對象 
代碼: 
import org.springframework.web.context.support.WebApplicationContextUtils; 
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); 
ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); 
ac1.getBean("beanId"); 
ac2.getBean("beanId"); 
說明: 
這種方式適合於採用Spring架構的B/S系統,通過ServletContext對象擷取ApplicationContext對象,然後在通過它擷取需要的類執行個體。 

上面兩個工具方式的區別是,前者在擷取失敗時拋出異常,後者返回null。 

其中 servletContext sc 可以具體 換成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession().getServletContext(); 另外,由於spring是注入的對象放在ServletContext中的,所以可以直接在ServletContext取出 WebApplicationContext 對象: WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); 

方法三:繼承自抽象類別ApplicationObjectSupport 
說明:抽象類別ApplicationObjectSupport提供getApplicationContext()方法,可以方便的擷取到ApplicationContext。 
Spring初始化時,會通過該抽象類別的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對象注入。 

方法四:繼承自抽象類別WebApplicationObjectSupport 
說明:類似上面方法,調用getWebApplicationContext()擷取WebApplicationContext 

方法五:實現介面ApplicationContextAware 
說明:實現該介面的setApplicationContext(ApplicationContext context)方法,並儲存ApplicationContext 對象。 
Spring初始化時,會通過該方法將ApplicationContext對象注入。 


在web應用中一般用ContextLoaderListener載入webapplication,如果需要在action之外或者control類之外擷取webapplication思路之一是,單獨寫個類放在static變數中,

spring mvc 下 applicationContext 和webApplicationContext

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.