zoukankan      html  css  js  c++  java
  • WebApplicationContextUtils源码

    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();
          }
        });
      }
    }
  • 相关阅读:
    SQL必知必会-笔记(五)函数
    软件测试面试题:系统中的图片不显示如何排查原因
    windows用浏览器访问linux目录文件
    记测试工作中一次印象深刻的事
    怎么快速适应新的测试工作?
    xshell如何导出日志文件和上传文件
    jmeter+fiddler高效率整理接口脚本
    python-用requests库处理form-data格式的参数
    软件自动化测试工程师面试题集锦(4)
    shell脚本批量检查某个或多个服务的端口和进程是否正常
  • 原文地址:https://www.cnblogs.com/shaohz2014/p/3682780.html
Copyright © 2011-2022 走看看