1. 程式人生 > >spring獲取webapplicationcontext,applicationcontext幾種方法詳解

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變數中,
類似於:
public class AppContext {

  private static AppContext instance;

  private AbstractApplicationContext appContext;

  public synchronized static AppContext getInstance() {
    if (instance == null) {
      instance = new AppContext();
    }
    return instance;
  }

  private AppContext() {
    this.appContext = new ClassPathXmlApplicationContext(
        "/applicationContext.xml");
  }

  public AbstractApplicationContext getAppContext() {
    return appContext;
  }


不過這樣,還是載入了2次applicationcontext,servlet一次,路徑載入一次;覺得不如直接用路徑載入,舍掉servlet載入
在網上也找了些其他說法:實現ApplicationContextAware,,, 介面,或者servletcontextAware介面,還要寫配置檔案。有的竟然要把配置檔案裡的listener,換成自己的類,這樣純粹多此一舉。不過有的應用不是替換,是在補一個listener,
我在一版的jpetstore(具體那一版不知道)裡發現了這個:
[web.xml]裡

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <listener>
        <listener-class>com.ibatis.jpetstore.util.SpringInit</listener-class>
    </listener>
其中SpringInit實現介面ServletContextListener 

package com.ibatis.jpetstore.util;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;


public class SpringInit implements ServletContextListener {
    

    private static WebApplicationContext springContext;
    
    public SpringInit() {
        super();
    }
    
    public void contextInitialized(ServletContextEvent event) {
        springContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
    }
    

    public void contextDestroyed(ServletContextEvent event) {
    }
    
    public static ApplicationContext getApplicationContext() {
        return springContext;
    }

    
}


在其中的一個bean的構造裡SpringInit獲取applicationcontext,程式碼:
  public OrderBean() {
    this(
            (AccountService) SpringInit.getApplicationContext().getBean("accountService"),
            (OrderService) SpringInit.getApplicationContext().getBean("orderService") );
  }

恩,這種在action,servlet之外的bean裡獲取applicationcontext的方法值得參考,應該有用