---
title: "Spring中的WebApplicationContext"
author: "kidfang"
date: "2009-06-01T14:36:49Z"
tags: ["Acegi", "Spring", "Web", "XML", "log4j"]
slug: "spring-webapplicationcontext"
draft: false
lastmod: "2009-06-01T14:40:44Z"
id: 399597
url: "https://kidfang.iteye.com/blog/399597"
---

参考

[http://hi.baidu.com/yawiki/blog/item/bff69822c1556bf3d6cae264.html](http://hi.baidu.com/yawiki/blog/item/bff69822c1556bf3d6cae264.html)

文中指出

在Web应用中，我们会用到WebApplicationContext，WebApplicationContext继承自ApplicationContext，先让我们看看在Web应用中，怎么初始化WebApplicationContext，在web.xml中定义:

&lt;context-param&gt;

    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
    &lt;param-value&gt;/WEB-INF/applicationContext.xml&lt;/param-value&gt;  
&lt;/context-param&gt;

&lt;listener&gt;  
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
&lt;/listener&gt;

 

实际配置如下，用的就是ContextLoaderListener

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "[http://java.sun.com/dtd/web-app\_2\_3.dtd](http://java.sun.com/dtd/web-app_2_3.dtd)"&gt;  
&lt;web-app&gt;  
  &lt;display-name&gt;XMNMS&lt;/display-name&gt;  
  &lt;context-param&gt;  
    &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;  
    &lt;param-value&gt;/WEB-INF/classes/log4j.properties&lt;/param-value&gt;  
  &lt;/context-param&gt;  
  &lt;context-param&gt;  
    &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
    &lt;param-value&gt;  
      /WEB-INF/applicationContext-basic.xml  
      /WEB-INF/applicationContext-security-acegi.xml  
    &lt;/param-value&gt;  
  &lt;/context-param&gt;

   &lt;filter-mapping&gt;  
    &lt;filter-name&gt;Acegi Filter Chain Proxy&lt;/filter-name&gt;  
    &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
  &lt;/filter-mapping&gt;

  &lt;!--  
  - Loads the root application context of this web app at startup.  
  - The application context is then available via  
  - WebApplicationContextUtils.getWebApplicationContext(servletContext).  
  --&gt;  
  &lt;listener&gt;  
    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;  
  &lt;/listener&gt;

  &lt;listener&gt;  
    &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;  
  &lt;/listener&gt;

  &lt;!--  
         The HttpSessionEventPublisher will publish  
         HttpSessionCreatedEvent and HttpSessionDestroyedEvent  
         to the WebApplicationContext  
  --&gt;  
  &lt;listener&gt;  
    &lt;listener-class&gt;net.sf.acegisecurity.ui.session.HttpSessionEventPublisher&lt;/listener-class&gt;  
  &lt;/listener&gt;

&lt;/web-app&gt;

 

 

页面中调用,如下  
&lt;%@ page import="org.springframework.web.context.WebApplicationContext"%&gt;  
&lt;%@ page import="org.springframework.web.context.support.WebApplicationContextUtils"%&gt;  
&lt;%@ page import="sample.service.IContactManager"%&gt;

&lt;%  
  WebApplicationContext wac =WebApplicationContextUtils.getRequiredWebApplicationContext(pageContext.getServletContext());  
  IContactManager contactManager= (IContactManager) wac.getBean("contactManager");  
  List contacts=null;  
  try{  
    //有授权  
    contacts=contactManager.getAll();  
  } catch(Exception e){  
    e.printStackTrace();  
  }

%&gt;