通常在方法中使用。在static中,會(huì)出現(xiàn)加載先后的順序,導(dǎo)致SpringContextUtil中的重寫方法還沒被調(diào)用,就先在其他的類中調(diào)用了getBean方法,報(bào)null。
Spring中,普通bean可以通過實(shí)現(xiàn)ApplicationContextAware得到ApplicationContext,需要重寫setApplicationContext。通過setApplicationContext將spring的applicationContext得到,那么spring是什么時(shí)候執(zhí)行setApplicationContext方法的呢? Spring源碼中ApplicationContextAwareProcessor.postProcessBeforeInitialization(),對(duì)繼承自ApplicationContextAware的bean進(jìn)行處理,調(diào)用其setApplicationContext。而ApplicationContextAwareProcessor是在spring容器start的時(shí)候生成的。
https://blog.csdn.net/u013147600/article/details/49616427 起因是睡覺的時(shí)候,我在想如果被面試問道:“你知道怎么可以獲取上下文嗎?”這個(gè)問題我感到很模糊,之前也寫過獲取上下文,但是記得好像有好幾種方法,覺得有點(diǎn)混淆了,所以就想自己好好整理下。 網(wǎng)上搜集的context上下文的幾種解釋:一: Context上下文主要用來從上文傳播對(duì)象到下文中,他是可以跨線程的。 就是說 class A中你把一個(gè)OBJ對(duì)象存放到了上下文容器中, 然后你以后的所有線程或你以下調(diào)用的所有類中都可以從上下文容器中取出 上面再class A中存放的OBJ對(duì)象。
二: 上下文即ServletContext,是一個(gè)全局的儲(chǔ)存信息的空間,服務(wù)器啟動(dòng),其就存在,服務(wù)器關(guān)閉,其才釋放。
所有用戶共用一個(gè)ServletContext。所以,為了節(jié)省空間,提高效率,ServletContext中,要放必須的、重要的、所有用戶需要共享的線程又是安全的一些信息。如,做一個(gè)購物類的網(wǎng)站,要從數(shù)據(jù)庫中提取物品信息,如果用session保存這些物品信息,每個(gè)用戶都訪問一便數(shù)據(jù)庫,效率就太低了;所以要用來Servlet上下文來保存,在服務(wù)器開始時(shí),就訪問數(shù)據(jù)庫,將物品信息存入Servlet上下文中,這樣,每個(gè)用戶只用從上下文中讀入物品信息就行了。 三: 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
....... 我的理解就歸結(jié)為"容器+環(huán)境". 獲取上下文:ServletContext 、ActionContext以及ServletActionContext上下文的介紹Spring上下文(ApplicationContext)方法一:ClassPathXmlApplicationContext --從classpath路徑加載配置文件,創(chuàng)建Bean對(duì)象 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); ClassName clazz =(ClassName)ctx.getBean("beanName"); 方法二:FileSystemXmlApplicationContext --從指定的目錄中加載 ApplicationContext ctx = new FileSystemXmlApplication Context("src/applicationContext.xml"); ClassName clazz =(ClassName)ctx.getBean("beanName");
方法三:Spring提供的工具類WebApplicationContextUtils獲取ApplicationContext對(duì)象(通過ServletContext對(duì)象獲得ApplicationContext對(duì)象,然后根據(jù)它獲得需要的類實(shí)例) HttpSession session =request.getSession(); ServletContext context = session.getServletContext(); //arg0.getSession().getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context); ClassName clazz =(ClassName)ctx.getBean("beanName");
上面兩個(gè)工具方式的區(qū)別是,前者在獲取失敗時(shí)拋出異常,后者返回null。
方法四:繼承自抽象類ApplicationObjectSupport 說明:抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext。 Spring初始化時(shí),會(huì)通過該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對(duì)象注入。
例如: import org.springframework.context.support.ApplicationObjectSupport; public class ContextOne extends ApplicationObjectSupport { ...... } ........ ContextOne one = new ContextOne(); one.getApplicationContext();
方法五:繼承自抽象類WebApplicationObjectSupport 說明:類似上面方法,調(diào)用getWebApplicationContext()獲取WebApplicationContext
例如: import org.springframework.web.context.support.WebApplicationObjectSupport; public class ContextOne extends WebApplicationObjectSupport { ....... } ........ ContextOne one = new ContextOne(); one.getApplicationContext(); 方法六:實(shí)現(xiàn)接口ApplicationContextAware當(dāng)一個(gè)類實(shí)現(xiàn)了ApplicationContextAware接口后,這個(gè)類就可以獲得Spring配置文件中的所引用到的bean對(duì)象。 說明:實(shí)現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對(duì)象。 Spring初始化時(shí),會(huì)通過該方法將ApplicationContext對(duì)象注入。
例如: package com.auth.util; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * 類:Context.java * 作者: LYX * 時(shí)間:2015-11-3 * 說明:通過接口ApplicationContextAware獲得spring上下文 */ public class Context implements ApplicationContextAware { private static ApplicationContext ctx; //設(shè)置ApplicationContext對(duì)象 public void setApplicationContext(ApplicationContext context) throws BeansException { // TODO Auto-generated method stub ctx=context; } //通過beanName獲得實(shí)例 public static Object getBean(String beanName) { return ctx.getBean(beanName); } }
|