Spring WebApplicationContext 介绍

版权声明:本文为博主原创文章,转载请指明文章出处! https://blog.csdn.net/u012385190/article/details/81368172

目录

 

一、ServletContext介绍

二、Spring Web应用上下文配置

三、总结


一、ServletContext介绍

javaee标准规定了,servlet容器需要在应用项目启动时,给应用项目初始化一个ServletContext作为公共环境容器存放公共信息,ServletContext中的信息都是由容器提供的。

在web项目中,web.xml文件我们通常有如下配置:

<context-param>
   <param-name>key</param-name>
   <param-value>value123</param-value>
</context-param>
<listener> 
   <listener-class>com.brolanda.contextlistener.listener.ContextListenerTest</listener-class>
</listener>

ServletContextListener的实现类代码如下(即上面的listener实现类):

public class ContextListenerTest implements ServletContextListener {
	//容器启动时执行该方法
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        ServletContext servletContext = servletContextEvent.getServletContext();
        String value = servletContext.getInitParameter("key");
        System.out.println(" ContextListenerTest contextInitialized , key value is " + value + " .......");
    }

    //容器结束时执行该方法
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
        System.out.println(" ContextListenerTest contextDestroyed ......");
    }
}

此时启动Web容器,执行流程如下:

1、启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件web.xml,读两个节点: <listener></listener> 和 <context-param></context-param>;
2、紧接着,容器创建一个ServletContext(上下文),在该应用内全局共享;
3、容器将<context-param></context-param>转化为键值对,并交给ServletContext;
4、容器创建<listener></listener>中的类实例,即创建监听,该监听器必须实现自ServletContextListener接口,如Log4jConfigListener,或者如上自定义实现类(如果不自定义实现,可以使用实现类ContextLoaderListener)
5、Web项目启动中,在监听类中ontextInitialized(ServletContextEvent event)初始化方法会被执行,在该方法中获取到ServletContext和全局参数;
6、得到这个context-param的值之后,你就可以做一些操作了。这个时候你的WEB项目还没有完全启动完成,这个动作会比所有的Servlet都要早。换句话说,这个时候,你对<context-param>中的键值做的操作,将在你的WEB项目完全启动之前被执行。
7、Web项目结束时,监听类中的contextDestroyed(ServletContextEvent event)方法会被执行;
简单来说流程就是:1、读配置文件节点-->2、创建ServletContext-->3、设置参数到Context中-->4、监听listener并执行初始化方法和销毁方法。

二、Spring Web应用上下文配置

Spring分别提供了用户启动WebApplicationContext的Servlet和Web容器监听器:
org.springframework.web.context.ContextLoaderListener
org.springframework.web.context.ContextLoaderServlet
所有版本的WEB容器都可以定义自启动的Servlet,但只有2.3及以上的版本的WEB容器才支持Web容器监听器,现在一般都使用Listener了。

spring为我们提供的IOC容器,需要我们指定容器的配置文件,然后由该监听器初始化并创建该容器。指定配置文件的地址及文件名称,一定要使用:contextConfigLocation作为参数名称。如下:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-INF/jason-servlet.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

该监听器,默认读取/WEB-INF/下的applicationContext.xml文件。但是通过context-param指定配置文件路径后,便会去你指定的路径下读取对应的配置文件,并进行初始化。项目启动时,便会执行类ContextLoaderListener的相关方法,创建WebApplicationContext(Web应用上下文)并以键值对形式存放与ServletContext中。

在web.xml中,可以配置多个Servlet,如下:

<servlet>
	<init-param>
        <param-name>param1</param-name>
        <param-value>avalible in servlet init()</param-value>
    </init-param>
    <servlet-name>ServletDemo</servlet-name>
	<servlet-class>demo.ServletDemo</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ServletDemo</servlet-name>
    <url-pattern>/servlet</url-pattern>
</servlet-mapping>

SpringIOC容器先根据监听初始化WebApplicationContext,然后再初始化web.xml中其他配置的servlet,为其初始化自己的上下文信息servletContext,并加载其设置的配置信息和参数信息到该上下文中,将WebApplicationContext设置为它的父容器。所以最后的关系是ServletContext包含了WebApplicationContext,WebApplicationContext包含了其他的Servlet上下文环境。如下图:

对于作用范围而言,在DispatcherServlet中可以引用由ContextLoaderListener所创建的ApplicationContext中的内容,而反过来不行。
当Spring在执行ApplicationContext的getBean时,如果在自己context中找不到对应的bean,则会在父ApplicationContext中去找。这也解释了为什么我们可以在DispatcherServlet中获取到由ContextLoaderListener对应的ApplicationContext中的bean。

三、总结

1、servlet容器需要在应用项目启动时,给应用项目初始化一个ServletContext作为公共环境容器存放公共信息。
2、WebApplicationContext,是继承于ApplicationContext的一个接口,扩展了ApplicationContext,是专门为Web应用准备的,它允许从相对于Web根目录的路径中装载配置文件完成初始化。
3、在非web应用下,Bean只有singleton和prototype两种作用域,WebApplicaitonContext为Bean添加了三个新的作用域:request/session/global session。
4、Spring分别提供了用户启动WebApplicationContext的Servlet和Web容器监听器(ContextLoaderServlet/ContextLoaderListener);
5、WebApplicationContext实现类:
5-1、XmlWebApplicationContext
采用xml配置,则Spring将使用XmlWebApplicationContext启动Spring容器,即通过XML文件为Spring容器提供Bean的配置信息;
5-2、AnnotationConfigWebApplicationContext
如果使用@Configure的java类提供配置信息,则需要在xml中进行相关配置,设置contextClass参数值为AnnotationConfigWebApplicationContext类,contextConfigLocation参数值则为使用了@Configure注解的类。ContextLoaderListener如果发现配置了contextClass参数,就是使用参数所指定的实现类初始化容器。ApplicationContext接口也有对应的实现类AnnotationConfigApplicationContext。

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

猜你喜欢

转载自blog.csdn.net/u012385190/article/details/81368172