Spring、SpringMVC版本及配置
一、Spring版本
Spring的最新版本是Spring 5.x,Spring 4.x的最后版本是Spring 4.3.x,会维护到2020年(Spring的GitHub主页对此有说明)。

二、SpringMVC
SpringMVC可以说是,应用了Spring的各种特性的一个MVC项目,它的核心Servlet是DispatcherServlet。
三、配置
各种Java框架一般都需要在web.xml中进行相关配置,一般都涉及到Listener、Filter、Servlet。
3.1 web.xml中配置
在Spring 3.1版本之前,在web.xml中配置DispatcherServlet是唯一的方式(同时声明映射):
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class> <init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/dispatcher-config.xml</param-value>
</init-param> <load-on-startup>1</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
**注释:load-on-startup is an integer value that specifies the order for multiple servlets to be loaded. So if you need to declare more than one servlet you can define in which order they will be initialized. Servlets marked with lower integers are loaded before servlets marked with higher integers.
3.2 web.xml和Java类中均可配置,即可混合配置
接下来,随着Servlet API 3.0的应用,web.xml中的配置不是必须的了,我们可以在Java类中配置DispatcherServlet:
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
这里的Java配置类取得的最终效果与3.1节中的相同。
但是我们仍然使用了一个XML文件:dispatcher-config.xml
3.3 100%的Java配置
通过对3.2节中的Java配置类进行重构,我们无需再使用XML文件来配置Dispatcher:
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext context
= new AnnotationConfigWebApplicationContext();
//context.register(AppConfig.class);
context.setConfigLocation("com.example.app.config");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
**注释:
The first thing we will need to do is create the application context for the servlet.
This time we will use an annotation based context so that we can use Java and annotations for configuration and remove the need for XML files like dispatcher-config.xml.
3.4 总结
Spring 3.2及其以上的版本,均可以采用以上三种方式进行配置。
官方推荐纯Java类来配置,更加简洁和灵活。但是有时候必须XML文件也是无法避免的
四、SpringBoot化繁为简
这些配置很繁琐,SpringBoot就是达到一键生成的效果!
Spring、SpringMVC版本及配置的更多相关文章
- idea spring+springmvc+mybatis环境配置整合详解
idea spring+springmvc+mybatis环境配置整合详解 1.配置整合前所需准备的环境: 1.1:jdk1.8 1.2:idea2017.1.5 1.3:Maven 3.5.2 2. ...
- SSM Spring +SpringMVC+Mybatis 整合配置 及pom.xml
SSM Spring +SpringMVC+Mybatis 配置 及pom.xml SSM框架(spring+springMVC+Mybatis) pom.xml文件 maven下的ssm整合配置步骤
- swagger-ui 系统配置过程(基于spring+springmvc+swagger+springfox配置 web-api 管理系统)
web工程部分框架信息:spring springmvc swagger springfox maven 参考文档:https://www.cnblogs.com/exmyth/p/7183753.h ...
- Spring+SpringMVC+MyBatis整合配置
前端控制器 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=& ...
- spring+springmvc+mybatis构建系统
今天和大家分享的是spring+springmvc+mybatis搭建框架的例子,说到这里不得不说现在市面上一流大公司还有很多用这种架子,创业型公司大部分都用springboot集成的mvc+myba ...
- 【Spring】Spring+SpringMVC+MyBatis框架的搭建
1,SSM的简介 SSM(Spring+SpringMVC+MyBatis)框架集由Spring.SpringMVC.MyBatis三个开源框架整合而成,常作为数据源较简单的web项目的框架. 其中s ...
- SpringMVC入门二:SSM整合(spring+springmvc+mybatis)
一.编程步骤 1.引入依赖 spring.springmvc.mybatis.mybatis-spring.mysql.druid.log4j.servlet-api.jstl.fastjson 2. ...
- 2.配置Spring+SpringMvc+Mybatis(分库or读写分离)--Intellij IDAE 2016.3.5
建立好maven多模块项目后,开始使用ssm传统的框架: 1.打开总工程下的pom.xml文件:添加如下代码: <!--全局的所有版本号定义--> <properties> & ...
- 用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(2 配置spring-dao和测试)
用IntelliJ IDEA 开发Spring+SpringMVC+Mybatis框架 分步搭建二:配置MyBatis 并测试(1 搭建目录环境和依赖) 四:在\resources\spring 下面 ...
随机推荐
- 第11组 Alpha冲刺(6/6)
第11组 Alpha冲刺(6/6) 队名 不知道叫什么团队 组长博客 https://www.cnblogs.com/xxylac/p/11913626.html 作业博客 https://edu ...
- mysql端口3306无法访问
mysql主备复制,show slave status显示IO一直connecting 一.查看了防火墙,已经处于关闭状态 二.查看使用的复制用户的权限,也已经开放 三.telnet访问另外一台机器端 ...
- (六)爬虫之使用selenium
selenium是使用javascript编写,主要用来进行web应用程序测试,在python爬虫中可以用来进行动态网页爬取,解决爬虫中的javascript渲染(执行js语句).总结记录下,以备后面 ...
- <JavaScript> 稳妥构造函数模式与工厂模式的区别
稳妥构造函数模式的代码应该是这样的: function Person(name, age, job) { var o = new Object(); // private members var na ...
- ShapeDrawable
形状的Drawable咯,定义基本的几何图形,如(矩形,圆形,线条等),根元素是<shape../> 节点比较多,相关的节点如下: ① <shape>: ~ visible:设 ...
- Redis CrackIT 入侵事件引发Linux 沦陷
▲针对全球6379端口的redis服务器做了扫描,结果如上图 如图开放在公网的redis的6379端口的ip总数有63443个.无密码认证的IP有43024个,在总数占比里达到67%.发现遭受到red ...
- windows下初安装xgboost
1.先检查一下自己的版本,如图python3.6,win64 我的之前环境是已经安装了anaconda. 2.去相关的网站下载 3.把下载的文件拷到此目录下(同pip在一个目录下) 4.cmd在此目录 ...
- STM32命名规则解析
- jenkins:集成sonar代码扫描+发送邮件
前提: Jenkins JDK 目录: 1.安装sonar插件:SonarQube Scanner for Jenkins 2.安装SonarQube 3.安装sonar-scanner ++++++ ...
- 微信小程序textarea层级过高(盖住其他元素)
根据官方文档,textarea 是原生组件 (https://developers.weixin.qq.com/miniprogram/dev/component/textarea.html),所谓原 ...