Spring3.x 升级至 Spring4.x 详解
1 升级依赖包
1.1 Maven 项目
1.1.1 更新 spring 依赖版本
打开 pom.xml,把所有 spring3.x 的版本号更新为 spring4.x。建议使用属性配置,形如:
<properties>
<spring.version>4.3.16.RELEASE</spring.version>
</properties>
这样就仅需一处升级即可,引用方式如下:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
...
1.1.2 升级 quartz
quartz 1.x 升级为 quartz 2.x:
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
quartz 配置文件中的 org.springframework.scheduling.quartz.CronTriggerBean 改为 org.springframework.scheduling.quartz.CronTriggerFactoryBean
1.1.3 升级 jackson
jackson 版本升级为 2.9.4:
<jackson.version>2.9.4</jackson.version>
涉及以下 5 个相关包:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
1.2 非 Maven 项目
非 Maven 的项目,要更新为 Spring4.x,过程比较痛苦,因为需要手动把相应的 JAR 包替换掉。
下面贴出可能涉及到、需要被替换掉的 JAR 包:



2 替换 spring jdbc 的 queryForInt 方法
queryForInt 方法已被弃用,请改为 queryForObject 方法:
queryForObject(String sql, Map<String, ?> paramMap, Class<T> requiredType)
调用示例:
int count=namedParameterJdbcTemplate.queryForObject(sql,
params, Integer.class);
3 SpringMVC 返回 JSON 格式
原来在 Spring3 中默认会返回 JSON 格式,然而在 Spring4 中可能会默认返回 XML 格式:

如果这里的响应消息格式变为 application/xml;charset=UTF-8,那么可以在 Controller 中的 @RequestMapping 中加入 produces = "application/json",明确指定返回 JSON 格式。
示例:
@RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json")
如果涉及的 Controller 较多(比如项目中存在大量的接口),那么可以修改 Spring 框架的源代码——org.springframework.http.MediaType:
public static void sortBySpecificityAndQuality(List<MediaType> mediaTypes) {
Assert.notNull(mediaTypes, "'mediaTypes' must not be null");
if (mediaTypes.size() > 1) {
Collections.sort(mediaTypes, new CompoundComparator<MediaType>(
MediaType.SPECIFICITY_COMPARATOR, MediaType.QUALITY_VALUE_COMPARATOR));
mediaTypes.set(0, MediaType.APPLICATION_JSON);//把 JSON 类型最为最优先的返回对象类型
}
}
这样所有的 @ResponseBody 的返回对象类型就都是 JSON 格式的对象啦O(∩_∩)O哈哈~
4 更新 XML 配置文件中的 xsd 版本号
把格式为 http://www.springframework.org/schema/xxx/spring-xxx-3.0.xsd 更改为 http://www.springframework.org/schema/xxx/spring-xxx-4.0.xsd,如果有的话。
作者:deniro
链接:https://www.jianshu.com/p/e52a7476ae95
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
Spring3.x 升级至 Spring4.x 详解的更多相关文章
- Spring4 JDBC详解
Spring4 JDBC详解 在之前的Spring4 IOC详解 的文章中,并没有介绍使用外部属性的知识点.现在利用配置c3p0连接池的契机来一起学习.本章内容主要有两个部分:配置c3p0(重点)和 ...
- Spring4 AOP详解
Spring4 AOP详解 第一章Spring 快速入门并没有对Spring4 的 AOP 做太多的描述,是因为AOP切面编程概念不好理解.所以这章主要从三个方面详解AOP:AOP简介(了解),基于注 ...
- Spring4 IOC详解
Spring4 IOC详解 上一章对Spring做一个快速入门的教程,其中只是简单的提到了IOC的特性.本章便对Spring的IOC进行一个详解.主要从三个方面开始:基于xml文件的Bean配置,基于 ...
- spring4配置文件详解
转自: spring4配置文件详解 一.配置数据源 基本的加载properties配置文件 <context:property-placeholder location="classp ...
- Spring3.0.5jar包用法详解 [转载]
Spring3.X以后jar包进行了重构,取消了原来2.X版本中的总的spring.jar包,而是把总包中的功能全部分开打包.正在向osgi靠拢. 各个jar包详解如下: 1. org.springf ...
- Spring3.0 核心jar包详解
org.springframework.aop 包含在应用中使用Spring的AOP特性时所需的类. org.springframework.asm Spring独立的ASM程序, Spring ...
- spring3.0的jar包详解
1. spring.jar 是包含有完整发布模块的单个jar 包. 2. org.springframework.aop 包含在应用中使用Spring的AOP特性时所需的类. 3. org.sprin ...
- Spring 配置详解
spring4配置文件详解 一.配置数据源 基本的加载properties配置文件 <context:property-placeholder location="classpath* ...
- Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍
Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍 spring集成 mybatis Spring4.x零配置框架搭建 两年前一直在做后 ...
随机推荐
- postgresql获取表最后更新时间(通过表磁盘存储文件时间)
一.创建获取表更新时间的函数 --获取表记录更新时间(通过表磁盘存储文件时间) create or replace function table_file_access_info( IN schema ...
- 二十八、python中的os模块
A.os模块:系统相关的(相对比较常用的有:os.stat('path/filename'),os.path.split(path),os.path.dirname(path),os.path.bas ...
- centos7安装kafka
1.官网或 wget 下载 kafka_2.12-2.2.0.tgz 二进制代码包 cd /home/tar wget http://mirror.bit.edu.cn/apache/kafka/2. ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_08 转换流_5_InputStreamReader介绍&代码实现
强转chart:类型 GBK
- jmeter之自动重定向和跟随重定向用法
jmeter工具里面有自动重定向和跟随重定向这2种选择,那么他们到底有啥区别呢? 目录 1.自动重定向和跟随重定向 2.举个例子 1.自动重定向和跟随重定向 01.3XX的请求一般要使用跟随重定向,2 ...
- 【Linux】limits.conf 不重启就生效或者不生效的原因
前阵子,我要用到使LInux的文件打开数为65534个,而且需要永久生效,于是将配置写到了: vim /etc/security/limits.conf * soft nofile 65534* ha ...
- mysql续集(查询部分)
mysql查询部分,从基础的查询到关键字,where子句,group by, order by, limit ,having,子查询分为from子查询和where子查询,左连接和右连接,内连接的连表查 ...
- JAVA总结--jvm
VM,Virtual Machine 即虚拟机,指通过软件模拟的具有完整硬件系统功能的.运行在一个完全隔离环境中的完整计算机系统. JVM,Java Virtual Machine 即Java虚拟机, ...
- JAVA总结--设计模式
三大类设计模式: 创建型模式,共五种:工厂方法模式.抽象工厂模式.单例模式.建造者模式.原型模式. 结构型模式,共七种:适配器模式.装饰器模式.代理模式.外观模式.桥接模式.组合模式.享元模式. 行为 ...
- Mysql8- Public Key Retrieval is not allowed
在使用 MySQL 8.0 时重启应用后提示 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Public ...