Spring MVC启动过程
来源:互联网 发布:最大公约数算法 java 编辑:程序博客网 时间:2024/06/02 16:25
以Tomcat为例,想在Web容器中使用Spirng MVC,必须进行四项的配置:
修改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> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springmvc.xml</param-value> <!---不配置的话 默认是court-servlet.xml --> </init-param> <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/applicationContext.xml</param-value></context-param><!-- 配置ContextLoaderListerner --><listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class></listener>
DispatcherServlet:前端处理器,接受的HTTP请求和转发请求的类。
applicationContext.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初始化。
1、在web.xml配置监听器ContextLoaderListener
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
ContextLoaderListener的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了 ServletContextListener这个接口,在web.xml配置这个监听器,启动容器时,就会默认执行它实现的方法。
2、部署applicationContext的xml文件
如何部署applicationContext的xml文件,如果在web.xml中不写任何参数配置信息,默认的路径是"/WEB-INF /applicationContext.xml,在WEB-INF目录下创建的xml文件的名称必须是applicationContext.xml。 如果是要自定义文件名可以在web.xml里加入contextConfigLocation这个context参数:
<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/applicationContext-*.xml</param-value></context-param>
在 里指定相应的xml文件名,如果有多个xml文件,可以写在一起并一“,”号分隔。上面的 applicationContext-*.xml采用通配符,比如这那个目录下有applicationContext-ibatis- base.xml,applicationContext-action.xml,applicationContext-ibatis-dao.xml 等文件,都会一同被载入。
由此可见applicationContext.xml的文件位置就可以有两种默认实现:
第一种:直接将之放到/WEB-INF下,之在web.xml中声明一个listener、
第二种:将之放到classpath下,但是此时要在web.xml中加入<context-param>
,用它来指明你的 applicationContext.xml的位置以供web容器来加载。按照Struts2 整合spring的官方给出的档案,写成:
<!-- Context Configuration locations for Spring XML files --><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value></context-param>
3、调用处
在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。看看它的API说明
第一段说明ContextLoader可以由 ContextLoaderListener和ContextLoaderServlet生成。如果查看ContextLoaderServlet的 API,可以看到它也关联了ContextLoader这个类而且它实现了HttpServlet。这个接口
第二段,ContextLoader创建的是 XmlWebApplicationContext这样一个类,它实现的接口是 WebApplicationContext->ConfigurableWebApplicationContext->ApplicationContext->
BeanFactory这样一来spring中的所有bean都由这个类来创建
IUploaddatafileManager uploadmanager = (IUploaddatafileManager) ContextLoaderListener.getCurrentWebApplicationContext().getBean(“uploadManager”);
- 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 学习笔记--启动过程
- 汇编基础学习
- MySQL 远程登录 和遇到问题解决
- Android Fragment
- tf.nn.rnn_cell.DrououtWrapper函数的用法
- x86虚拟化
- Spring MVC启动过程
- Liunx新手配置教程Java版
- Android开发入门之为应用添加多个Activity与参数传递
- Zookeeper实例Curator API-使用Curator删除节点
- 想要了解大数据信用风险评分,这篇文章值得细读!(上)
- Java中值得你小心的事(一)——继承
- 2.区块链该看哪些会议论文
- JDBC包装类
- win7主机和linux虚拟机网络PING不通——解决