前言

web.xml中标签的加载顺序:<context-param> > <listener> (spring的相关工作) > filter >servlet(springmvc的相关工作)

web.xml中spring的核心ContextLoaderListener初始化的上下文和springmvc的核心DispatcherServlet初始化的上下文关系

  1. ContextLoaderListener初始化的上下文加载的Bean是对于整个应用程序共享的   :一般如DAO层、Service层Bean;
  2. DispatcherServlet初始化的上下文加载的Bean只对Spring Web MVC有效的Bean,如Controller该初始化上下文应该只加载Web相关组件

javaEE项目启动过程:首先加载Spring上下文环境配置文件,然后加载SpringMVC配置文件。

spring和springMVC整合后,可以取消掉web.xml中spring的listener配置

contextLoaderListener创建的上下文context默认配置就是applicationContext.xml,而DispatchServlet创建的context默认配置是spring-servlet.xml

contextLoaderListener先创建上下文–app上下文,后者DipatcherServlet将前者的上下文作为父亲上下文进行继承然后再创建自己的上下文–mvc上下文,当一个bean在mvc上下文中无法获取就会溯源到父上下文–app上下文获取。

因为同时有父子上下文,所以假如两个配置中都配置了同样的bean或者contextLoaderListener和DispatcherServlet中都使用同一个配置,那么就会产生初始化两次的囧况。

  • 如果只有 Spring mvc 的一个 Servlet,listener 可以不用。
  • 但是如果用了Shiro 等,Shiro 用到的 Spring 的配置必须在 listener 里加载。
  • 一般 Dao, Service 的 Spring 配置都会在 listener 里加载,因为可能会在多个 Servlet 里用到,因为父子 Context 的可见性问题,防止重复加载所以在 listener 里加载。

配置详情

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1" metadata-complete="true"> <!--默认欢迎页-->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> <!-- 启动Spring容器,声明应用范围(整个WEB项目)内的上下文初始化参数 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--如果没有显式声明,则 系统默认 在WEB-INF/applicationContext.xml -->
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<!--ContextLoaderListener的作用:为项目提供了spring支持,初始化了Ioc容器。
就是启动Web容器时,读取在contextConfigLocation中定义的xml文件,
自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象,然后将这个对象放置在ServletContext的属性里-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!-- 配置spring字符编码为utf-8 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--参数forceEncoding的作用:
以前只是对request字符编码起作用,现在如果将forceEncoding设为true也会影响到response中的字符编码-->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>false</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> <!-- 配置DispatcherServlet表示,该工程将采用springmvc的方式 -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置springMVC需要加载的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc.xml</param-value>
</init-param>
<!-- 应用启动时,加载servlet -->
<load-on-startup>1</load-on-startup>
<!-- 支持异步处理 -->
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<!-- 默认匹配所有的请求 -->
<url-pattern>/</url-pattern>
<!-- 拦截Action -->
<!--<url-pattern>*.do</url-pattern>-->
</servlet-mapping> <!-- session 时间 -->
<session-config>
<session-timeout>30</session-timeout>
</session-config> </web-app>

配置文件一web.xml的更多相关文章

  1. Spring中,applicationContext.xml 配置文件在web.xml中的配置详解

    一.首先写一下代码结构. 二.再看web.xml中的配置情况. <?xml version="1.0" encoding="UTF-8"?> < ...

  2. spring的配置文件在web.xml中加载的方式

    web.xml加载spring配置文件的方式主要依据该配置文件的名称和存放的位置不同来区别,目前主要有两种方式. 1.如果spring配置文件的名称为applicationContext.xml,并且 ...

  3. Spring项目的配置文件们(web.xml context servlet springmvc)

    我们的spring项目目前用到的配置文件包括1--web.xml文件,这是java的web项目的配置文件.我理解它是servlet的配置文件,也就是说,与spring无关.即使你开发的是一个纯粹jsp ...

  4. 关于j2ee工程发布到was上后,部分更新,例修改web.xml配置文件不起作用的原因解析【转】

    在WAS中,应用的配置是从config/cells....目录下读取:而资源从/installedApps目录下读取 故当配置文件(例web.xml)发生改变时,只更新应用程序资源文件/install ...

  5. spring 在web.xml 里面如何使用多个xml配置文件

    1, 在web.xml中定义 contextConfigLocation参数.spring会使用这个参数加载.所有逗号分割的xml.如果没有这个参数,spring默认加载web-inf/applica ...

  6. web.xml详解

    web.xml的作用 web.xml,一个Tomcat工程中最重要的配置文件.web.xml没有其实也可以----只要你确定你的项目里面不需要任何过滤器.监听器.Servlet等等.我试了一下,没有w ...

  7. 【转】web.xml中的contextConfigLocation在spring中的作用

    一.spring中如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocation参数,Spring会使用这个参数去加载所有逗号分隔的xml文件,如果没有这个参数,sp ...

  8. (转)web.xml中的contextConfigLocation在spring中的作用

    (转)web.xml中的contextConfigLocation在spring中的作用   一.Spring如何使用多个xml配置文件 1.在web.xml中定义contextConfigLocat ...

  9. spring 和springmvc 在 web.xml中的配置

    (1)问题:如何在Web项目中配置Spring的IoC容器? 答:如果需要在Web项目中使用Spring的IoC容器,可以在Web项目配置文件web.xml中做出如下配置: <!-- Sprin ...

随机推荐

  1. 数据可视化echart

    刚接到这个一脸懵逼,到现在还算有点眉目,先上个图庆祝一下,哈哈,开心.... 最近没时间,等待更新..........

  2. 制作一个自己的xhprof测试平台

    1 1.首先安装php开发环境,比如lnmp. 2.安装xhprof ps: 记住从github上面下载(https://github.com/phacility/xhprof), 不要从pecl.p ...

  3. 2019-8-31-dotnet-通过-HttpClient-下载文件同时报告进度的方法

    title author date CreateTime categories dotnet 通过 HttpClient 下载文件同时报告进度的方法 lindexi 2019-08-31 16:55: ...

  4. 关闭swap

    #(1)临时关闭swap分区, 重启失效;   swapoff  -a #(2)永久关闭swap分区 sed -ri 's/.*swap.*/#&/' /etc/fstab

  5. Python 3.3 IDLE 删除键出现空格卡顿

    Python 3.3 IDLE 删除文字时会出现"□",造成删除键卡顿 原因:使用的是百度输入法,由于输入法的编码方式不同,对IDLE造成了不兼容 换成了搜狗输入法问题解决.... ...

  6. Aira搭建

    Aira下载地址 https://github.com/aria2/aria2/releases AriaNg下载地址 https://github.com/mayswind/AriaNg/relea ...

  7. python数据读取路径为啥要用双反斜杠?

    Window下python读取数据路径可以有三种表示方式: (1)'c:\\a.txt' ——>转义的方式.表示这里\\是一个普通\字符,不容易出错(2)r'c:\a.txt' ——>声明 ...

  8. TPCx-BB官宣最新世界纪录,阿里巴巴计算力持续突破

    2019年9月17日,TPC官宣Alibaba Cloud MaxCompute认证结果.同月26日,杭州云栖大会阿里巴巴宣布了这一成绩,飞天大数据平台计算引擎MaxCompute成为全球首个TPCx ...

  9. thinkphp 替换入口

    3.2版本支持根据当前的运行环境生成Lite文件,可以替换框架的入口文件或者应用入口文件,提高运行效率. 我们的建议是在生产环境中关闭调试模式后生成Lite文件.注意,目前SAE平台不支持直接生成Li ...

  10. 【Codeforces Round #589 (Div. 2) D】Complete Tripartite

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 其实这道题感觉有点狗. 思路大概是这样 先让所有的点都在1集合中. 然后随便选一个点x,访问它的出度y 显然tag[y]=2 因为和他相连了嘛 ...