Spring MVC启动过程
来源:互联网 发布:性功能障碍 知乎 编辑:程序博客网 时间:2024/06/10 12:02
以Tomcat为例,想在Web容器中使用Spirng MVC,必须进行四项的配置:
修改web.xml,添加servlet定义、编写servletname-servlet.xml( servletname是在web.xm中配置DispactherServlet时使servlet-name的值) 、配置contextConfigLocation初始化参数、配置ContextLoaderListerner。
01
<!-- servlet定义 -->
02
<
servlet
>
03
<
servlet-name
>court</
servlet-name
>
04
<
servlet-class
>org.springframework.web.servlet.DispatcherServlet</
servlet-class
>
05
<
load-on-startup
>1</
load-on-startup
>
06
</
servlet
>
07
08
<
servlet-mapping
>
09
<
servlet-name
>court</
servlet-name
>
10
<
url-pattern
>/</
url-pattern
>
11
</
servlet-mapping
>
12
13
<!-- 配置contextConfigLocation初始化参数 -->
14
<
context-param
>
15
<
param-name
>contextConfigLocation</
param-name
>
16
<
param-value
>/WEB-INF/court-service.xml</
param-value
>
17
</
context-param
>
18
19
<!-- 配置ContextLoaderListerner -->
20
<
listener
>
21
<
listener-class
>org.springframework.web.context.ContextLoaderListener</
listener-class
>
22
</
listener
><
span
style
=
"font-family:'sans serif, tahoma, verdana, helvetica';font-size:x-small;"
><
span
style
=
"line-height:19px;"
> </
span
></
span
>
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使用。
1 0
- Spring MVC启动过程
- Spring MVC启动过程
- Spring MVC启动过程
- Spring MVC启动过程
- Spring MVC 启动过程
- Spring MVC启动过程
- Spring MVC启动过程
- Spring MVC启动过程
- Spring MVC启动过程
- spring mvc 启动过程
- Spring MVC启动过程
- spring MVC 启动过程详解
- Spring MVC - 启动过程详解
- Spring MVC启动整体过程
- spring mvc 的启动过程
- 【Spring MVC】Spring MVC启动过程源码分析
- Spring MVC启动过程之ContextLoaderListener初始化
- SPRING Web MVC 学习笔记--启动过程
- url二次编码
- Android中 Matrix类
- Java IO - ObjectInputStream&ObjectOutputStream
- 操作系统覆盖技术和交换技术
- 06-图1 列出连通集
- Spring MVC启动过程
- Markdown使用体会
- HDU-5698-瞬间移动(杨辉三角)
- 插入排序
- Volley与WebView共享Cookie
- java之装饰者模式
- PAT 1018剪刀石头布
- hive学习之四:hive文件格式以及压缩编码
- js正则融合字符串