獲取Spring容器中的Bean

Spring中的ApplicationContexts能夠被限制在不一樣的做用域。在web框架中,每一個DispatcherServlet有它本身的WebApplicationContext,它包含了DispatcherServlet配置所須要的bean。DispatcherServlet 使用的缺省BeanFactory是XmlBeanFactory,而且DispatcherServlet在初始化時會在你的web應用的WEB-INF目錄下尋找[servlet-name]-servlet.xml文件。DispatcherServlet使用的缺省值可使用servlet初始化參數修改,web

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

Spring的DispatcherServlet有一組特殊的bean,用來處理請求和顯示相應的視圖。這些bean包含在Spring的框架裏,(可選擇)能夠在WebApplicationContext中配置,配置方式就象配置其它bean的方式同樣。這些bean中的每個都在下面被詳細描述。待一下子,咱們就會提到它們,但這裏僅僅是讓你知道它們的存在以便咱們繼續討論DispatcherServlet。對大多數bean,都提供了缺省值,全部你沒必要要擔憂它們的值。網絡



// 一下來源於網絡
app


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

ApplicationContext,WebApplicationContext 是Spring的BeanFactory,從名字中就能夠知道區別拉,一個是支持web特性的BeanFactory。webapp

Spring獲取WebApplicationContext與ApplicationContext的幾種方法:ide

方法一:在初始化時保存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變量中工具

以下:ui

1.this

public class SpringContextUtil implements ApplicationContextAware{
 
 private static ApplicationContext applicationContext;//Spring應用上下文環境
 @Override
 public void setApplicationContext(ApplicationContext applicationcontext)
   throws BeansException {
  // TODO Auto-generated method stub
  SpringContextUtil.applicationContext=applicationcontext;
 }
  public static Object getBean(String name){
  return applicationContext.getBean(name);
 }
}

//applicationContext.xml 配置
// <bean id="SpringContextUtil" class="com.zjd.util.SpringContextUtil"/>

SpringContextUtil.getBean("bean id");

2.

WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext);

ctx.getBean("bean id");

相關文章
相關標籤/搜索