web項目如何獲取容器中的bean對象?

from:http://blog.163.com/sir_876/blog/static/11705223201111544523333/
spring通過在web.xml 中配置ContextLoaderListener 來加載context配置文件,在DispatcherServlet中也可以來加載spring context配置文件,那麼這兩個有什麼區別呢。

ContextLoaderListener中加載的context成功後,spring 將 applicationContext存放在ServletContext中key值爲"org.springframework.web.context.WebApplicationContext.ROOT"的attribute 中。 (servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context));可以通過 WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 或WebApplicationContextUtils.getWebApplicationContext(servletContext)方法來獲取對應的applicationContext。

DispatcherServlet加載的context成功後,如果 publishContext屬性的值設置爲true的話(缺省爲true) 會將applicationContext存放在 org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。

例如 web.xml中配置如下
Xml代碼
<servlet>
<servlet-name>mvcServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring/config/applicationContextMVC.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

則對應的applicationContext的attribute key值爲org.springframework.web.servlet.FrameworkServlet.CONTEXT.mvcServlet。

在每次request請求時,DispatcherServlet會將此applicationContext存放在request中attribute 值爲 org.springframework.web.servlet.DispatcherServlet.CONTEXT中 (request.setAttribute(WEB_APPLICATION_CONTEXT_ATTRIBUTE,getWebApplicationContext());)。可以通過 RequestContextUtils.getWebApplicationContext 或 WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname) 方法 來獲取對應的applicationContext。

從上面的分析可以看出,DispatcherServlet所加載的applicationContext可以認爲是mvc私有的context,由於保存在servletContext中的key值與通過ContextLoaderListener加載進來的applicationContext使用的 key值不相同,因此如果只使用DispatcherServlet加載context的話,如果程序中有地方使用 WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext) 來試圖獲取applicationContext時,就會拋出"No WebApplicationContext found: no ContextLoaderListener registered?"的exception。

那我們怎麼取了?
ApplicationContext ac = (WebApplicationContext) this.getServeltContext().getAttribute("org.springframework.web.servelt.FrameworkServlet.CONTEXT.這裏填寫web.xml配置的servlet的名稱");

http://pbcljf.blog.163.com/blog/static/340340192010513537424/
方法一:在初始化時保存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的方法值得參考,應該有用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章