spring中WebApplicationContextUtils类说明

spring中WebApplicationContextUtils类说明

WebApplicationContextUtils是一个抽象类,其提供了一个很便利的方法来获取spring应用的上下文即WebApplicationContext。

其中的静态方法getWebApplicationContext(ServletContext sc),提供一个ServletContext 类型参数即可。

其原理十分简单,在spring容器初始化的方法org.springframework.web.context.ContextLoader.initWebApplicationContext(ServletContext)中

通过servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);已经将WebApplicationContext的实例放入ServletContext 中了。

然后在工具类的org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(ServletContext)中就可以通过传入的ServletContext参数获取到WebApplicationContext实例了。


package org.springframework.web.context.support;


import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestScope;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.SessionScope;


public abstract class WebApplicationContextUtils
{
  public static WebApplicationContext getRequiredWebApplicationContext(ServletContext sc)
    throws IllegalStateException
  {
    WebApplicationContext wac = getWebApplicationContext(sc);
    if (wac == null) {
      throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");
    }
    return wac;
  }


  public static WebApplicationContext getWebApplicationContext(ServletContext sc)
  {
    return getWebApplicationContext(sc, WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
  }


  public static WebApplicationContext getWebApplicationContext(ServletContext sc, String attrName)
  {
    Assert.notNull(sc, "ServletContext must not be null");
    Object attr = sc.getAttribute(attrName);
    if (attr == null) {
      return null;
    }
    if ((attr instanceof RuntimeException)) {
      throw ((RuntimeException)attr);
    }
    if ((attr instanceof Error)) {
      throw ((Error)attr);
    }
    if ((attr instanceof Exception)) {
      IllegalStateException ex = new IllegalStateException();
      ex.initCause((Exception)attr);
      throw ex;
    }
    if (!(attr instanceof WebApplicationContext)) {
      throw new IllegalStateException("Context attribute is not of type WebApplicationContext: " + attr);
    }
    return (WebApplicationContext)attr;
  }


  public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory)
  {
    beanFactory.registerScope("request", new RequestScope());
    beanFactory.registerScope("session", new SessionScope(false));
    beanFactory.registerScope("globalSession", new SessionScope(true));


    beanFactory.registerResolvableDependency(ServletRequest.class, new ObjectFactory() {
      public Object getObject() {
        RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
        if (!(requestAttr instanceof ServletRequestAttributes)) {
          throw new IllegalStateException("Current request is not a servlet request");
        }
        return ((ServletRequestAttributes)requestAttr).getRequest();
      }
    });
    beanFactory.registerResolvableDependency(HttpSession.class, new ObjectFactory() {
      public Object getObject() {
        RequestAttributes requestAttr = RequestContextHolder.currentRequestAttributes();
        if (!(requestAttr instanceof ServletRequestAttributes)) {
          throw new IllegalStateException("Current request is not a servlet request");
        }
        return ((ServletRequestAttributes)requestAttr).getRequest().getSession();
      }
    });
  }
}

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值