spring mvc 为什么这么多xml
spring web mvc 处理流程
Architecture 
web.xml (webapp必要配置)
作用:spring web mvc 使用dispatcherServlet 分发request,一般我们都需要一个web.xml 来定义这项工作。
servlet-mapping 中定义工作包括:
- servlet-name :用于匹配名字到 [servlet-name]-servlet.xml (默认的规则:路径位于:/WEBContent/WEB-INF/[servlet-name]-servlet.xml)
- url-pattern : 定义哪些url 需要servlet去处理
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
其他:
- context-param 的 contextConfigLocation:如果不适用默认规则:自己指定路径来设置Spring容器加载配置文件路径, 如下例子使用applicationContext.xml 可以多个xml
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,
/WEB-INF/part2.xml</param-value>
</init-param>
- listener-class : 为了能在加载spring 的 bean 配置,设置 Listener
- listen-class 为org.springframework.web.context.ContextLoaderListener
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
servlet 定义的比较固定:
- servlet-class : 定义哪种具体的servlet class(org.springframework.web.servlet.DispatcherServlet) 去做
- servlet-name :与 web.xml 的servlet-mapping 对应的 servlet-name
dispatcher-servlet.xml / [servlet-name]-servlet.xml (webapp 非必要配置)
作用:初始化 bean
比较典型的例子:
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
applicationContext.xml (webapp 非必要配置)
bean 的class 是 org.springframework.web.servlet.view.InternalResourceViewResolver
如果需要初始化bean 对象的成员属性property,则定义其具体属性property name & value
dispatcher-servlet.xml 与 applicationContext.xml 的差别:
共同:两者对webapp 其实都是非必须
applicationContext.xml 定义bean 的 root webapp context
dispatcher-servlet.xml 定义的bean 给一个叫dispatcher(可以是其他名字)的 webapp context
据说 普通webapp context 可以引用 root webapp context
也即:dispatcher-servlet.xml 可以引用applicationContext.xml 定义 context。但反之不行
吐槽:
这么多xml 真的挺烦的,而且还有pom.xml。而且xml 都是比较冗长,使用习惯了其他如nodejs的动态语言,写起来会真的对这些冗长的配置很恼火。
对于pom.xml, gradle 可以稍微释放 xml
对于上述这么多xml,spring-boot 可以释放 这些这么多配置的问题。尤其在:我只不过是想写个简单的rest、单一职责的微服务而已。
另外一方面,这么多xml,IDE是有工具去做简化这些工作的。譬如:
- pom.xml 的下载管理是可以通过IDE dependencies tab -> Add 去搜做的。
- Spring Bean configuration file(beans.xml)是可以通过IDE beans tab -> New Bean 去配置的。
然而还是觉得比较麻烦。。。
参考:Spring - MVC Framework Tutorial(http://www.tutorialspoint.com/spring/spring_web_mvc_framework.htm)
spring mvc architecture(http://www.slideshare.net/RaviKantSoni2/spring-framework-3-session1)
Spring的web.xml配置
(http://book.51cto.com/art/200909/151039.htm)
explain dispatcher-servlet.xml, applicationContext.xml, web.xml (https://www.quora.com/How-will-you-explain-dispatcher-servlet-xml-applicationContext-xml-web-xml-and-spring-servlet-xml-to-a-NOVICE-J2EE-Java-programmer-and-relationship-between-these-xmls-in-a-Spring-Web-App)
difference between ApplicationContext and WebApplicationContext (http://stackoverflow.com/questions/11708967/what-is-the-difference-between-applicationcontext-and-webapplicationcontext-in-s)
spring mvc 为什么这么多xml的更多相关文章
- 如何用Java类配置Spring MVC(不通过web.xml和XML方式)
DispatcherServlet是Spring MVC的核心,按照传统方式, 需要把它配置到web.xml中. 我个人比较不喜欢XML配置方式, XML看起来太累, 冗长繁琐. 还好借助于Servl ...
- spring Mvc 执行原理 及 xml注解配置说明 (六)
Spring MVC 执行原理 在 Spring Mvc 访问过程里,每个请求都首先经过 许多的过滤器,经 DispatcherServlet 处理; 一个Spring MVC工程里,可以配置多个的 ...
- spring mvc实现Restful返回xml格式数据
最近,想在自己的小项目中搭建一个Restful风格的服务接口api,项目用的spring mvc 3,听说spring mvc本身就能十分方便的支持restful的实现,于是查询了下资料,果然非常强大 ...
- spring启动,spring mvc ,要不要xml配置,基于注解配置
老项目是09-11年搞的,用的是spring+struts2,没有用注解,全xml配置.web.xml中也配置了一大堆. 现在启动新项目,在项目中用spring+springmvc ,主要用注解,也用 ...
- Spring mvc项目的web.xml以及注释
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp ...
- IntelliJ idea创建Spring MVC的Maven项目
参考:http://my.oschina.net/gaussik/blog/385697?fromerr=Pie9IlFV 创建Maven Web项目 菜单File->New Project可进 ...
- spring 整合 spring mvc
需要进行 Spring 整合 SpringMVC 吗 ? 还是否需要再加入 Spring 的 IOC 容器 ? 是否需要再 web.xml 文件中配置启动 Spring IOC 容器的 Context ...
- spring mvc(1):基础入门
依赖 pom.xml ( maven ) <properties> <spring.version>3.0.5.RELEASE</spring.version> ...
- 【Spring】搭建最简单的Spring MVC项目
每次需要Spring MVC的web项目测试一些东西时,都苦于手头上没有最简单的Spring MVC的web项目,现写一个. > 版本说明 首先要引入一些包,Spring的IOC.MVC包就不用 ...
随机推荐
- Rancher2.0中使用外置NFS存储部署Nginx实验
目录: 1.环境准备工作 1.1 准备好Rancher2.0集群环境 1.2 准备好外部NFS服务器 2.Rancher2.0中使用NFS存储的方法 2.1 在集群中创建持久卷(PV) 2.2 在项目 ...
- SpringBoot MyBatis 配置多数据源 (静态多个)
转载地址:https://www.jianshu.com/p/118ca1d5ecf9?utm_campaign=haruki&utm_content=note&utm_medium= ...
- 跟敲MySQL数据库实用教程
MySQL数据库操作 创建数据库 create database if not exists test; 查看数据库 show databases; show databases [ like wil ...
- Pyhon中运算符的使用
1. a & b python中的&延续了C/C++的含义,表示位运算. 例如 3 & 4:3&5:6&7 3 & 4 = (011)2 & ( ...
- 牛客练习赛23CD
链接:https://www.nowcoder.com/acm/contest/156/C 来源:牛客网 题目描述 托米完成了1317的上一个任务,十分高兴,可是考验还没有结束 说话间1317给了托米 ...
- 2015-10-13 jQuery5实例
jQuery(5) 一.扑克牌切换 <body> <div class="ig ig1"><img src="image/1.jpg&q ...
- python dpkt SSL 流tcp payload(从三次握手开始到application data)和证书提取
# coding: utf-8 #!/usr/bin/env python from __future__ import absolute_import from __future__ import ...
- 用SQL语句创建和删除Access数据库中的表;添加列和删除列
用SQL语句创建和删除Access数据库中的表;添加列和删除列 Posted on 2009-08-11 13:42 yunbo 阅读(1240) 评论(0) 编辑 收藏 用SQL语句创建和删除Acc ...
- Linux c 获取cpu使用率
部分代码改编自来自http://blog.csdn.net/primeprime/article/details/41458731 主要的原理就是获取top -n 1 | grep Cpu执行的结果, ...
- python 多个脚本
1.增删改查haproxy.conf配置文件 1.查询输入:www.oldboy1.com 2.删除输入:{'backend': 'www.oldboy2.org','record':{'server ...