Spring MVC启动过程之ContextLoaderListener初始化
2014-05-04 10:20
501 查看
以Tomcat为例,想在Web容器中使用Spirng MVC,必须进行四项的配置:
修改web.xml,添加servlet定义、编写servletname-servlet.xml( servletname是在web.xm中配置DispactherServlet时使servlet-name的值) 、配置contextConfigLocation初始化参数、配置ContextLoaderListerner。
DispatcherServlet:前端处理器,接受的HTTP请求和转发请求的类。
court-servlet.xml:定义WebAppliactionContext上下文中的bean。
contextConfigLocation:指定Spring IoC容器需要读取的定义了非web层的Bean(DAO/Service)的XML文件路径。
ContextLoaderListener:Spring MVC在Web容器中的启动类,负责Spring IoC容器在Web上下文中的初始化。
Spring MVC启动过程大致分为两个过程:1、ContextLoaderListener初始化,实例化IoC容器,并将此容器实例注册到ServletContext中。2、DispatcherServlet初始化。
Web容器调用contextInitialized方法初始化ContextLoaderListener,在此方法中,ContextLoaderListener通过调用继承自ContextLoader的initWebApplicationContext方法实例化Spring Ioc容器。
initWebApplicationContext方法进行的操作如图1:
以上在实例化Spring IoC容器的过程中,最主要的两个方法是createWebApplicationContext和configureAndRefreshWebApplicationContext方法。
createWebApplicationContext方法用于返回XmlWebApplicationContext实例,即Web环境下的Spring IoC容器。
configureAndRefreshWebApplicationContext用于配置XmlWebApplicationContext,读取web.xml中通过contextConfigLocation标签指定的XML文件,实例化XML文件中配置的bean,并在上一步中实例化的容器中进行注册。
完成以上两步的操作后,Spring MVC会将XmlWebApplicationContext实例以属性的方式注册到ServletContext中,属性的名称由WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE指定,默认值为:WebApplicationContext.class.getName() + ".ROOT"。此Spring 容器是ROOT上下文,供所有的Spring MVC Servlcet使用。
----------
浅析ContextLoaderListener
大家可能对下面这段代码再熟悉不过了
没错,这就是在web应用加载完之前先加载ContextLoaderListener,然后通过这个类里面的某个方法去加载spring配置文件
下面我们新建一个自己的ContextLoaderListene
新建立一个java类型,继承ContextLoaderListener,然后实现他的两个主要的方法contextInitialized,contextDestroyed;在contextInitialized,contextDestroyed这两个方法里面你就可以做自己想做的事情了,然后再web.xml里面添加一个listener,将自己的class加进去即可
java代码如下:
web.xml代码如下
在写的过程中我发现一个问题:contextDestroyed这个方法怎么都不会被调用,后来不小心重新启动项目,却意外的调用了此方法,甚是不解!!!
修改web.xml,添加servlet定义、编写servletname-servlet.xml( servletname是在web.xm中配置DispactherServlet时使servlet-name的值) 、配置contextConfigLocation初始化参数、配置ContextLoaderListerner。
<!-- servlet定义 --> <servlet> <servlet-name>court</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>court</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置contextConfigLocation初始化参数 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/court-service.xml</param-value> </context-param> <!-- 配置ContextLoaderListerner --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
DispatcherServlet:前端处理器,接受的HTTP请求和转发请求的类。
court-servlet.xml:定义WebAppliactionContext上下文中的bean。
contextConfigLocation:指定Spring IoC容器需要读取的定义了非web层的Bean(DAO/Service)的XML文件路径。
ContextLoaderListener:Spring MVC在Web容器中的启动类,负责Spring IoC容器在Web上下文中的初始化。
Spring MVC启动过程大致分为两个过程:1、ContextLoaderListener初始化,实例化IoC容器,并将此容器实例注册到ServletContext中。2、DispatcherServlet初始化。
ContextLoaderListener初始化
Web容器调用contextInitialized方法初始化ContextLoaderListener,在此方法中,ContextLoaderListener通过调用继承自ContextLoader的initWebApplicationContext方法实例化Spring Ioc容器。initWebApplicationContext方法进行的操作如图1:
以上在实例化Spring IoC容器的过程中,最主要的两个方法是createWebApplicationContext和configureAndRefreshWebApplicationContext方法。
createWebApplicationContext方法用于返回XmlWebApplicationContext实例,即Web环境下的Spring IoC容器。
configureAndRefreshWebApplicationContext用于配置XmlWebApplicationContext,读取web.xml中通过contextConfigLocation标签指定的XML文件,实例化XML文件中配置的bean,并在上一步中实例化的容器中进行注册。
完成以上两步的操作后,Spring MVC会将XmlWebApplicationContext实例以属性的方式注册到ServletContext中,属性的名称由WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE指定,默认值为:WebApplicationContext.class.getName() + ".ROOT"。此Spring 容器是ROOT上下文,供所有的Spring MVC Servlcet使用。
----------
浅析ContextLoaderListener
大家可能对下面这段代码再熟悉不过了
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
没错,这就是在web应用加载完之前先加载ContextLoaderListener,然后通过这个类里面的某个方法去加载spring配置文件
下面我们新建一个自己的ContextLoaderListene
新建立一个java类型,继承ContextLoaderListener,然后实现他的两个主要的方法contextInitialized,contextDestroyed;在contextInitialized,contextDestroyed这两个方法里面你就可以做自己想做的事情了,然后再web.xml里面添加一个listener,将自己的class加进去即可
java代码如下:
1 package com.software.aaa; 2 3 import javax.servlet.ServletContextEvent; 4 5 import org.springframework.web.context.ContextLoaderListener; 6 7 public class Test extends ContextLoaderListener{ 8 9 @Override 10 public void contextInitialized(ServletContextEvent event) { 11 // TODO Auto-generated method stub 12 System.out.println("contextInitialized............."); 13 } 14 15 @Override 16 public void contextDestroyed(ServletContextEvent event) { 17 // TODO Auto-generated method stub 18 System.out.println("contextDestroyed............."); 19 } 20 }
web.xml代码如下
1 <listener> 2 <listener-class>com.software.aaa.Test</listener-class> 3 </listener>
在写的过程中我发现一个问题:contextDestroyed这个方法怎么都不会被调用,后来不小心重新启动项目,却意外的调用了此方法,甚是不解!!!
相关文章推荐
- Spring MVC启动过程(1):ContextLoaderListener初始化
- Spring MVC启动过程(1):ContextLoaderListener初始化
- Spring MVC启动过程(1):ContextLoaderListener初始化
- Spring MVC启动过程(1):ContextLoaderListener初始化
- SpringWeb ContextLoaderListener 初始化过程
- Spring Framework源码读取-ContextLoaderListener启动过程
- maven项目 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- SpringMvc项目 Tomacat启动错误 ContextLoaderListener
- maven的java web项目启动找不到Spring ContextLoaderListener的解决办法
- 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- ContextLoaderListener和Spring MVC中的DispatcherServlet学习
- Intellij IDEA 启动项目ClassNotFoundException:org.springframework.web.context.ContextLoaderListener
- 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- maven项目 启动报错:java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- SpringMVC容器初始化 ContextLoaderListener
- 启动tomcat出现java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
- SSM初始化:org.springframework.web.context.ContextLoaderListener
- 读Spring的源代码三:ContextLoaderListener和DispatcherServlet的加载过程
- Spring初始化:org.springframework.web.context.ContextLoaderListener的作用
- maven的web项目启动找不到Spring ContextLoaderListener