Servletcontext,ApplicationContext和DispatcherServlet间的区别_spring

在springmvc中有很多概念,其中Servletcontext,ApplicationContext和DispatcherServlet是被提到最多的概念。下文将介绍这些概念的作用与区别。(spring boot中相同)

1.Servletcontext

说到Servletcontext,首先需要了解浏览器请求web的过程。


  1. 浏览器发送http请求到web容器。并将请求发送给tomcat等web容器。
  2. tomcat将http请求封装成httpServletRequest并发送给web项目。
    Servletcontext,ApplicationContext和DispatcherServlet间的区别_tomcat_02
    而Servletcontext就是tomcat给web项目创建的全局环境。他有以下特点。
  3. 全局共享数据。
  4. 包含着web.xml里面的初始值。

2.ApplicationContext

在web.xml中,有以下代码。

<listener>    
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

我们可以点击进去看源代码。然后进入。

@Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}

然后在点击进入。

public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - " +
"check whether you have multiple ContextLoader* definitions in your web.xml!");
}

Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
if (logger.isInfoEnabled()) {
logger.info("Root WebApplicationContext: initialization started");
}
long startTime = System.currentTimeMillis();

try {
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
if (!cwac.isActive()) {
// The context has not yet been refreshed -> provide services such as
// setting the parent context, setting the application context id, etc
if (cwac.getParent() == null) {
// The context instance was injected without an explicit parent ->
// determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
cwac.setParent(parent);
}
configureAndRefreshWebApplicationContext(cwac, servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

ClassLoader ccl = Thread.currentThread().getContextClassLoader();
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
else if (ccl != null) {
currentContextPerThread.put(ccl, this.context);
}

if (logger.isDebugEnabled()) {
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
}
if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
}

return this.context;
}
catch (RuntimeException ex) {
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
}
catch (Error err) {
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
}
}

这段代码很简单,我们可以看到在servletContext中以key-value方式放入了一个WebApplicationContext对象,同时这也是spring的IOC环境。

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

3.DispatcherServlet

在进行以上流程之后,web.xml继续配置其他servlet,如DispatcherServlet(大家都拿这个举例。。。。),然后会找到WebApplicationContext。并把它作为自己的父上下文。并在WebApplicationContext中加载。

下图大概表明他们之间的关系。

Servletcontext,ApplicationContext和DispatcherServlet间的区别_java_03