web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="3.0"
  3.    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  5.  
  6.  
  7.    <context-param>
  8.       <param-name>log4jConfigLocation</param-name>
  9.       <param-value>classpath:config/properties/log4j.properties</param-value>
  10.    </context-param>
  11.  
  12.    <listener>
  13.       <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  14.    </listener>
  15.  
  16. <!-- context-param中配置的contextConfigLocation只对ContextLoaderListener有效 不会对DispatcherServlet有效 -->
  17. <!-- DispatcherServlet用来加载mvc相关的bean ContextLoaderListener用来加载其他bean -->
  18. <!-- ContextLoaderListener会生成一个spring的context 注:DispatcherServlet不会识别@controller注解-->
  19.    <context-param>
  20.       <param-name>contextConfigLocation</param-name>
  21.       <param-value>/WEB-INF/config/spring-bean/*.xml</param-value>
  22.    </context-param>
  23.  
  24.    <listener>
  25.       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  26.    </listener>
  27.  
  28.  
  29. <!-- 配置DispatcherServlet load-on-startup要為1 它會根据contextConfigLocation生成一個spring的context-->
  30.    <servlet>
  31.       <servlet-name>spring</servlet-name>
  32.       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  33.       <init-param>
  34.          <param-name>contextConfigLocation</param-name>
  35.          <param-value>/WEB-INF/config/spring-servlet.xml</param-value>
  36.       </init-param>
  37.       <load-on-startup>1</load-on-startup>
  38.    </servlet>
  39.  
  40.    <servlet-mapping>
  41.       <servlet-name>spring</servlet-name>
  42.       <url-pattern>/</url-pattern>
  43.    </servlet-mapping>
  44.  
  45.  
  46.  
  47.  <filter>
  48.         <filter-name>encodingFilter</filter-name>
  49.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  50.         <init-param>
  51.             <param-name>encoding</param-name>
  52.             <param-value>UTF-8</param-value>
  53.         </init-param>
  54.         <init-param>
  55.             <param-name>forceEncoding</param-name>
  56.             <param-value>true</param-value>
  57.         </init-param>
  58.     </filter>
  59.     <filter-mapping>
  60.         <filter-name>encodingFilter</filter-name>
  61.         <url-pattern>/*</url-pattern>
  62.     </filter-mapping>
  63.  
  64. </web-app>

ContextLoaderListener中加载的context成功后,spring 将 applicationContext存放在ServletContext中key值为"org.springframework.web.context.WebApplicationContext.ROOT"的attribute中。

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

可以通过WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)或WebApplicationContextUtils.getWebApplicationContext(servletContext)方法来获取对应的applicationContext。

DispatcherServlet加载的context成功后,如果 publishContext属性的值设置为true的话(缺省为true) 会将applicationContext存放在org.springframework.web.servlet.FrameworkServlet.CONTEXT. + (servletName)的attribute中。

在每次request请求时,DispatcherServlet会将此applicationContext存放在request中,

可以通过 RequestContextUtils.getWebApplicationContext 或 WebApplicationContextUtils.getWebApplicationContext(servletContext,attrname)方法 来获取对应的applicationContext。

在配置的过程中尽量让DispatcherServlet和ContextLoaderListener职责分离,避免一个bean被加载两次。

或者只使用DispatcherServlet(没有试过 应该可行)。

从web.xml的配置可知ContextLoaderListener会先于DispatcherServlet创建ApplicationContext,DispatcherServlet在创建ApplicationContext时会先找到由ContextLoaderListener所创建的ApplicationContext,再将后者的ApplicationContext作为参数传给DispatcherServlet的ApplicationContext的setParent()方法。

当Spring在执行ApplicationContext的getBean时,如果在自己context中找不到对应的bean,则会在父ApplicationContext中去找。这也解释了为什么我们可以在DispatcherServlet中获取到由ContextLoaderListener对应的ApplicationContext中的bean。

spring-servlet.xml:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans.xsd
  7.        http://www.springframework.org/schema/context
  8.        http://www.springframework.org/schema/context/spring-context.xsd
  9.        http://www.springframework.org/schema/tx
  10.        http://www.springframework.org/schema/tx/spring-tx.xsd
  11.           http://www.springframework.org/schema/mvc
  12.        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  13.  
  14.     <!-- 配置扫描的包 这里配置的包和它的下级包都会被扫描 这样不仅会扫描@controller @service @repository 还会扫描@component-->
  15.     <context:component-scan base-package="com.zjf" />
  16.  
  17.  
  18.      <!-- 注册HandlerMapper、HandlerAdapter两个映射类 这是spring为@Controller分发请求所必须的 同时也是表单数据转换为对象(如xml json等)必须的-->
  19.     <mvc:annotation-driven />
  20.  
  21.  
  22.      <!-- 访问静态资源 如果没有这个配置 那么静态的js css文件将会报错 因为在web.xml中我们配置了<url-pattern>/</url-pattern> -->
  23.     <mvc:default-servlet-handler />
  24.  
  25.     <!-- 视图解析器 这里使用InternalResourceViewResolver -->
  26.     <bean
  27.         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  28.         <property name="prefix" value="/WEB-INF/view/"></property>
  29.         <property name="suffix" value=".jsp"></property>
  30.     </bean>
  31.  
  32. </beans>

Spring mvc项目的web.xml以及注释的更多相关文章

  1. 如何用Java类配置Spring MVC(不通过web.xml和XML方式)

    DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...

  2. java ssm框架入门(三)正式项目的web.xml配置

    一个正规的上线的web.xml的配置. <?xml version="1.0" encoding="UTF-8"?> <web-app id= ...

  3. 使用 java替换web项目的web.xml

    创建一个接口: package my.web; public interface SpringWeb { void config(); } 实现类: package my; import my.web ...

  4. IDEA创建maven项目的web.xml头

    使用IDEA创建maven项目骨架是webapp时,软件自动创建的web.xml文件是2.3版本的,不能使用el表达式,所以可以手动换成4.0的文件头. <?xml version=" ...

  5. maven web项目的web.xml报错The markup in the document following the root element must be well-formed.

    maven项目里面的web.xml开头约束是这样的 <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  6. Idea创建Maven Web项目的web.xml版本问题

    问题描述:创建Maven Web项目时,选择MavenWebapp模板时,自动生成的web.xml文件版本为1.4,如图所示 如何才能修改为常用的4.0版本的xml文件呢? 这个文件是从Maven仓库 ...

  7. 基于Spring + Spring MVC + Mybatis 高性能web构建

    基于Spring + Spring MVC + Mybatis 高性能web构建 一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJs,做了大量的研究,对前后端交互 ...

  8. 一个电商项目的Web服务化改造

    一个电商项目的Web服务化改造 项目,早期是随便瞎做的,没啥架构,连基本的设计也没. 有需求,实现需求,再反复修改. 大致就是这么做的. 最近,项目要重新架构,和某boss协商的结果是,采用阿里开源的 ...

  9. DispatcherServlet和ContextLoaderListener,还有spring+servlet3.0 无web.xml启动问题

    上篇提到: 关于spring +springmvc中两个spring应用上下文(DispatcherServlet和ContextLoaderListener)的问题,挺让人迷糊的. 他们都是加载Be ...

随机推荐

  1. Python的环境的搭建

    下载Python软件安装,配置环境变量 给Eclipse安装pydev插件即可

  2. JAVA初级面试题,附个人理解答案

    一,面向对象的特征:1.抽象 包括数据抽象跟行为抽象,将对象共同的特征取出形成一个类2.继承 被继承类为基类/超类,继承类为子类/派生类3.封装 多次使用道德数据或方法,封装成类,方便多次重复调用4. ...

  3. web开发(三) 会话机制,Cookie和Session详解

    在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6422391.html>,在此仅供学习参考之用. 一.会话 ...

  4. [Nova] Failed to get shared "write" lock Is another process using the image?

    目录 文章目录 目录 问题 解决 问题 [root@overcloud-compute-0 ~]# /usr/bin/python2 -m oslo_concurrency.prlimit --as= ...

  5. Python学习-------变量和简单的数据类型(String)

    1.变量命名和使用 变量命名规则:a.变量名只能包含(字母    数字   下划线),且变量不能以数字开头,例如:变量 s_1(正确),变量1_s(错误) b.变量名不能包含空格,可以使用下划线来间隔 ...

  6. dapper使用时性能优化

    数据库中类型 Area 数据库类型 varchar dapper 来操作数据库,不能直接写 sql      Area=@Area)   //dapper 对C#中的字符串类型 默认是对应数据库nva ...

  7. Golang基础(6):go的net/http用法

    http包提供了HTTP客户端和服务端的实现 一:http客户端的几种方法 1. func (c *Client) Get(url string) (resp *Response, err error ...

  8. JS中document对象 && window对象

    所有的全局函数和对象都属于Window对象的属性和方法. 区别: 1.window 指窗体.Window 对象表示浏览器中打开的窗口. document指页面.document是window的一个子对 ...

  9. USACO1.5 Mother's Milk【搜索】

    题目传送门 这道题还记得是我当年学广搜的时候做过. 如今再做,做了一个$dfs$版本的,比较简单,直接搞就可以了. 广搜的话,用结构体保存,然后塞到$queue$里面就可以了. /* ID: Star ...

  10. 2019JAVA第七次实验报告

    Java实验报告 班级 计科二班 学号 20188442 姓名 吴怡君 完成时间 2019.10.25 评分等级 实验任务详情: 完成火车站售票程序的模拟. 要求: (1)总票数1000张: (2)1 ...