细说ServletContext、WebApplicationContext、Servlet的初始化

细说ServletContext、WebApplicationContext、Servlet的初始化

浏览器请求发送给服务器的过程

1.浏览器发送http请求到web容器。比如请求发送给tomcat等web容器。

2.tomcat将http请求封装成httpServletRequest并发送给web项目。而

Servletcontext就是tomcat给web项目创建的全局环境。他有以下特点:

  1. 全局共享数据。
  2. 包含着web.xml里面的初始值。

1.ServletContext对象的生命周期

​ ServletContext代表是一个web应用的环境(上下文)对象,ServletContext象 内部封装是该web应用的信息,一个web应用只有一个。

创建:该web应用被加载(服务器启动或发布web应用(前提,服务器启动状 态))

销毁:web应用被卸载(服务器关闭,移除该web应用)

2.ServletContext的运行流程

通过一个例子来说明:

通过上面知晓:当Servlet容器启动时会加载web.xml配置文件,并且创建一个ServletContext对象,将web.xml中的参数信息存放在ServletContext对象中。

执行流程:

web.xml在<context-param></context-param>标签中声明应用范围内的初始化参数

1.启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml.读两个节点: <listener></listener><context-param></context-param>

2.紧接着,容器创建一个ServletContext(上下文)。在该应用内全局共享。

3.容器将<context-param></context-param>转化为键值对,并交给ServletContext.

4.容器创建<listener></listener>中的类实例,即创建监听.该监听器必须实现自ServletContextListener接口

5.在监听中会有contextInitialized(ServletContextEvent event)初始化方法

在这个方法中获得ServletContext = ServletContextEvent.getServletContext();

“context-param的值” = ServletContext.getInitParameter(“context-param的键”);

6.得到这个context-param的值之后,你就可以做一些操作了.注意,这个时候你的WEB项目还没有完全启动完成.这个动作会比所有的Servlet都要早.换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行.

web.xml中有两种参数的定义

  1. 全局参数(ServletContext):定义在context-param标签中
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
        classpath:spring/applicationContext-*.xml
    </param-value>
</context-param>
<listener>
	<listener-class>         org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
   2. Servlet参数:比如前端控制器的inti-param标签中
<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
	</servlet>

第一种参数在Servlet中可以通过getServletContext().getInitParameter("")得到。

第二种参数只能在servlet的init()方法中通过this.getInitParameter("")取得。

3.ContextLoaderListener的源码分析

​ 前面简单的说明了ContextLoaderListener的作用:可以获得ServletContext的对象和Context-param的值。接下来详细说明下这个。

​ 首先,我们从web.xml中开始,在web.xml中我们首先配置的是contextLoaderListener,它的作用就是启动web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会自动执行它实现的contextInitialized()方法。这样就能够在客户端请求之前向ServletContext中添加任意的对象。

​ 在ServletContextListener中的核心逻辑便是初始化WebApplicationContext实例并存放至ServletContext中。


public void contextInitialized(ServletContextEvent event) {
		this.contextLoader = createContextLoader();
		if (this.contextLoader == null) {
			this.contextLoader = this;
		}
		this.contextLoader.initWebApplicationContext(event.getServletContext());
}

​ 通过源码可以看出contextInitialized()中创建了一个contextLoader对象,然后该对象调用了initWebApplicationContext()。

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) {
				configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext)this.context, 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;
		}
	}

initWebApplicationContext()主要是WebApplicationContext创建的过程:首先,验证WebApplicationContext的存在性,通过查看ServletContext实例中是否有对应key的属性验证WebApplicationContext是否已经创建过实例。如果没有通过**createWebApplicationContext()**方法来创建实例,并存放至ServletContext中。

	protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
        //
		Class<?> contextClass = determineContextClass(sc);
		if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
			throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
					"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
		}
		ConfigurableWebApplicationContext wac =
				(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
		return wac;
	}

​ 从源码中可以详细的看到return wac,是通过BeanUtils.instantiateClass()来创建实例,但是其中传递了一个contextClass,该对象是通过determineContextClass方法创建的,接下来分析该方法的源码:

	protected Class<?> determineContextClass(ServletContext servletContext) {
        //首先从servletContext对象中获取值
		String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
		if (contextClassName != null) {
			try {
        //该值不为null通过forName()来装载该类
				return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
			}
			catch (ClassNotFoundException ex) {
				throw new ApplicationContextException(
						"Failed to load custom context class [" + contextClassName + "]", ex);
			}
		}
		else {
        //servletContext中没有该对象时
			contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
			try {
				return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
			}
			catch (ClassNotFoundException ex) {
				throw new ApplicationContextException(
						"Failed to load default context class [" + contextClassName + "]", ex);
			}
		}
	}

​ 该方法很详细的解释了是如何获取CONTEXT_CLASS_PARAM(类名参数)。

首先如果contextClassName不为空时就通过forName来装载这个类,很明显第一次启动web容器时contextClassName为空。

contextClassName为空时通过defaultStrategies.getProperty()获得实现类的名称,而defaultStrategies是在ContextLoader类的静态代码块中赋值的。具体的途径,则是读取ContextLoader类的同目录下的ContextLoader.properties属性文件来确定的。

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

​ 找到该文件后发现里面存储了一个键值对,故会加载这个类org.springframework.web.context.support.XmlWebApplicationContext

​ 也就是说,在初始化的过程中,程序会首先读取ContextLoader类的同目录下的属性文件ContextLoader.properties,并根据其中的配置提取将要实现WebApplicationContext接口的实现类,并根据这个类通过反射进行实例的创建。

综合以上的代码:ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置了这个监听器,启动容器时,就会默认执行它实现的contextInitialized()方法初始化WebApplicationContext实例(XmlWebApplicationContext),并放入到ServletContext中。由于在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

​ 如果在web.xml中不写任何参数配置信息,默认的路径是**/WEB-INF/applicationContext.xml**,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml

​ 如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
        classpath:spring/applicationContext-*.xml
    </param-value>
</context-param>

4.Servlet的初始化

​ 上面说到了配置ServletContext并初始化,那web.xml中的Servlet是如何初始化的呢?

  1. Servlet容器启动,为应用创建一个全局上下文环境:ServletContext对象
  2. 容器通过web.xml中的ContextLoaderListener监听器来创建一个WebApplicationContext上下文环境对象(IOC容器)加载context-param中指定的配置文件信息到WebApplicationContext对象中,该对象在ServletContext中是以键值对的形式保存的。
  3. 容器初始化web.xml中配置的servlet,为其初始化自己的上下文信息servletContext,并加载其设置的配置信息到该上下文中。将WebApplicationContext设置为它的父容器。
  4. 此后的所有servlet的初始化都按照3步中方式创建,初始化自己的上下文环境,将WebApplicationContext设置为自己的父上下文环境。

通过一张图来了解上面的意思:

在这里插入图片描述

通过上面的图很清楚的了解到:

  1. ServletContext是全局环境:里面包含了其他的上下文环境对象通过键值对的形式保存。
  2. WebApplicationContext是所有Servlet的父环境:**也就是当Spring在执行getBean时,如果在自己的context(上下文环境)中找不到,那么就会去父类中的context寻找。(反过来就不行!)**这也解释了为什么我们可以在DispatcherServlet中获取到由ContextLoaderListener对应的ApplicationContext中的bean。

参考:https://www.cnblogs.com/brolanda/p/4265597.html

https://blog.csdn.net/qq_38410730/article/details/79426673

  • 9
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值