Servletcontext,ApplicationCont

小知识,大挑战!本文正在参与“程序员必备小知识”创作活动。

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

1.Servletcontext

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

  1. 浏览器发送http请求到web容器。并将请求发送给tomcat等web容器。
  2. tomcat将http请求封装成httpServletRequest并发送给web项目。
    在这里插入图片描述
    而Servletcontext就是tomcat给web项目创建的全局环境。他有以下特点。
  3. 全局共享数据。
  4. 包含着web.xml里面的初始值。

2.ApplicationContext

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

1
2
3
js复制代码 <listener>    
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

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

1
2
3
4
js复制代码    @Override
public void contextInitialized(ServletContextEvent event) {
initWebApplicationContext(event.getServletContext());
}

然后在点击进入。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
js复制代码     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环境。

1
js复制代码servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

3.DispatcherServlet

在进行以上流程之后,web.xml继续配置其他servlet,如DispatcherServlet(大家都拿这个举例。。。。),然后会找到WebApplicationContext。并把它作为自己的父上下文。并在WebApplicationContext中加载。
下图大概表明他们之间的关系。
在这里插入图片描述

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%