1. 程式人生 > >java-幾種上下文以及獲取Spring的ApplicationContext的幾種方法(整理)

java-幾種上下文以及獲取Spring的ApplicationContext的幾種方法(整理)

起因是睡覺的時候,我在想如果被面試問道:“你知道怎麼可以獲取上下文嗎?”這個問題我感到很模糊,之前也寫過獲取上下文,但是記得好像有好幾種方法,覺得有點混淆了,所以就想自己好好整理下。

網上搜集的context上下文的幾種解釋:

Context上下文主要用來從上文傳播物件到下文中,他是可以跨執行緒的。 就是說  class A中你把一個OBJ物件存放到了上下文容器中, 然後你以後的所有執行緒或你以下呼叫的所有類中都可以從上下文容器中取出 上面再class A中存放的OBJ物件。
二:
上下文即ServletContext,是一個全域性的儲存資訊的空間,伺服器啟動,其就存在,伺服器關閉,其才釋放。
所有使用者共用一個ServletContext。所以,為了節省空間,提高效率,ServletContext中,要放必須的、重要的、所有使用者需要共享的執行緒又是安全的一些資訊。如,做一個購物類的網站,要從資料庫中提取物品資訊,如果用session儲存這些物品資訊,每個使用者都訪問一便資料庫,效率就太低了;所以要用來Servlet上下文來儲存,在伺服器開始時,就訪問資料庫,將物品資訊存入Servlet上下文中,這樣,每個使用者只用從上下文中讀入物品資訊就行了。
三: A naming service associates names with objects. An association between a name and an object is called a binding, and a set of such bindings is called a context. A name in a context can be bound to another context that uses the same naming conventions; the bound context is called a subcontext. For example, in a filesystem, a directory (such as 
/temp) is a context that contains bindings between filenames and objects that the system can use to manipulate the files (often called file handles). If a directory contains a binding for another directory (e.g., /temp/javax), the subdirectory is a subcontext. 
(http://blog.csdn.net/centurymagus/article/details/2025455)

四:java中context上下文 http://blog.csdn.net/xiaokui008/article/details/8454949
....... 我的理解就歸結為"容器+環境".

獲取上下文:

ServletContext 、ActionContext以及ServletActionContext上下文的介紹

Spring上下文(ApplicationContext)

方法一:ClassPathXmlApplicationContext --從classpath路徑載入配置檔案,建立Bean物件 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); ClassName clazz =(ClassName)ctx.getBean("beanName"); 方法二:FileSystemXmlApplicationContext  --從指定的目錄中載入 ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml"); ClassName clazz =(ClassName)ctx.getBean("beanName");
方法三:Spring提供的工具類WebApplicationContextUtils獲取ApplicationContext物件(通過ServletContext物件獲得ApplicationContext物件,然後根據它獲得需要的類例項) HttpSession session =request.getSession();
ServletContext context = session.getServletContext(); //arg0.getSession().getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context); ClassName clazz =(ClassName)ctx.getBean("beanName");
上面兩個工具方式的區別是,前者在獲取失敗時丟擲異常,後者返回null。

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

例如: import org.springframework.context.support.ApplicationObjectSupport; public class ContextOne extends ApplicationObjectSupport {     ...... } ........ ContextOne one = new ContextOne();   one.getApplicationContext();


方法五:繼承自抽象類WebApplicationObjectSupport
說明:類似上面方法,呼叫getWebApplicationContext()獲取WebApplicationContext

例如: import org.springframework.web.context.support.WebApplicationObjectSupport; public class ContextOne extends WebApplicationObjectSupport {     ....... }
........ ContextOne one = new ContextOne();   one.getApplicationContext();

方法六:實現介面ApplicationContextAware 當一個類實現了ApplicationContextAware介面後,這個類就可以獲得Spring配置檔案中的所引用到的bean物件。 說明:實現該介面的setApplicationContext(ApplicationContext context)方法,並儲存ApplicationContext 物件。
Spring初始化時,會通過該方法將ApplicationContext物件注入。

例如:
package com.auth.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /**  * 類:Context.java  * 作者: LYX  * 時間:2015-11-3  * 說明:通過介面ApplicationContextAware獲得spring上下文  */ public class Context implements ApplicationContextAware {  private static ApplicationContext ctx; //設定ApplicationContext物件  public void setApplicationContext(ApplicationContext context)    throws BeansException {   // TODO Auto-generated method stub    ctx=context;  }  //通過beanName獲得例項  public static Object getBean(String beanName)  {   return ctx.getBean(beanName);  } }
-----------------------------