Spring 监听ContextLoaderListener
2015-06-05 14:10
281 查看
以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使用。
修改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使用。
相关文章推荐
- 【汇总】eclipse相关问题
- spring security3的filter链
- Java偏向锁
- Spring+Quartz实现定时任务的配置方法(插曲)
- 20150605面试汇总--jdk常用package
- eclipse tab空格设置
- Struts2学习笔记1
- 用java抓取cnproxy代理服务器地址
- 在Spring3中使用注解(@Scheduled)创建计划任务
- struts2 .do .action等后缀访问问题
- Maven的Setting.xml配置默认JDK,本地仓库,repositories
- java环境变量 maven 环境变量配置
- 06-图6. 公路村村通(30) java
- struts2入门实例
- Eclipse快捷键 10个最有用的快捷键
- 一个不错的地址:包含spring框架、其他框架 等java相关的
- J2SE使用JAVA原生代码直接调用WebService Result
- Java语言描述:分支限界法之单源最短路径问题(全网独家)
- java调用规则引擎
- 随机数 java.util.Random与java.lang.Math.Random()-Java