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 包:

 
quartz 相关
 
jackson 相关
 
Spring 相关

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 详解的更多相关文章

  1. Spring4 JDBC详解

    Spring4 JDBC详解 在之前的Spring4 IOC详解 的文章中,并没有介绍使用外部属性的知识点.现在利用配置c3p0连接池的契机来一起学习.本章内容主要有两个部分:配置c3p0(重点)和 ...

  2. Spring4 AOP详解

    Spring4 AOP详解 第一章Spring 快速入门并没有对Spring4 的 AOP 做太多的描述,是因为AOP切面编程概念不好理解.所以这章主要从三个方面详解AOP:AOP简介(了解),基于注 ...

  3. Spring4 IOC详解

    Spring4 IOC详解 上一章对Spring做一个快速入门的教程,其中只是简单的提到了IOC的特性.本章便对Spring的IOC进行一个详解.主要从三个方面开始:基于xml文件的Bean配置,基于 ...

  4. spring4配置文件详解

    转自: spring4配置文件详解 一.配置数据源 基本的加载properties配置文件 <context:property-placeholder location="classp ...

  5. Spring3.0.5jar包用法详解 [转载]

    Spring3.X以后jar包进行了重构,取消了原来2.X版本中的总的spring.jar包,而是把总包中的功能全部分开打包.正在向osgi靠拢. 各个jar包详解如下: 1. org.springf ...

  6. Spring3.0 核心jar包详解

    org.springframework.aop  包含在应用中使用Spring的AOP特性时所需的类. org.springframework.asm   Spring独立的ASM程序, Spring ...

  7. spring3.0的jar包详解

    1. spring.jar 是包含有完整发布模块的单个jar 包. 2. org.springframework.aop 包含在应用中使用Spring的AOP特性时所需的类. 3. org.sprin ...

  8. Spring 配置详解

    spring4配置文件详解 一.配置数据源 基本的加载properties配置文件 <context:property-placeholder location="classpath* ...

  9. Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍

    Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍 spring集成 mybatis Spring4.x零配置框架搭建 两年前一直在做后 ...

随机推荐

  1. linux 挂载磁盘指令

    fdisk -l    (先df -h,如果没有xvdb盘信息,则敲这条指令) fdisk /dev/xvdb (进入对话状态,一问一答,结束后要保存w或者删除q) mkfs.ext3 /dev/xv ...

  2. 大数据笔记(十八)——Pig的自定义函数

    Pig的自定义函数有三种: 1.自定义过滤函数:相当于where条件 2.自定义运算函数: 3.自定义加载函数:使用load语句加载数据,生成一个bag 默认:一行解析成一个Tuple 需要MR的ja ...

  3. hashcode、equals、HashMap间的关系

    1.从Object说起package com.hallo.collection; public class ObjectDemo { public static void main(String[] ...

  4. 开源认证组件汇总 Kerberos和CAS

    一.Kerberos 1.Kerberos原理和工作机制 概述:Kerberos的工作围绕着票据展开,票据类似于人的驾驶证,驾驶证标识了人的信息,以及其可以驾驶的车辆等级. 1.1 客户机初始验证   ...

  5. 记一次SQL Server delete语句的优化过程

    今天测试反应问题,性能测试环境一个脚本执行了3个小时没有出结果,期间其他dba已经建立了一些索引但是没有效果. 语句: DELETE T  from License T  WHERE exists ( ...

  6. Git remotes/origin/pr/* 分支清理,代码回退等

    代码在gitHub上托管,每次git pull完后,用git branch -a都可以看到一堆remotes/origin/pr/*分支: 可以通过两种方式去除: 1,修改git的config文件找到 ...

  7. So the type system doesn’t feel so static.

    object wb{ def main(args:Array[String]){ println("Happy everyday!DATA-CENTER!") println(ne ...

  8. 小程序mpvue中动态切换echarts图表

    如果你用mpvue,而且还想用echarts,那么这个包你可以以来一下 https://github.com/F-loat/mpvue-echarts 考虑到多个页面都休要用,所以抽出来作为一个组件, ...

  9. Jenkins简介&邮箱配置

    一.Jenkins基本介绍: Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能.--摘自百科 二 ...

  10. Week 5 - 529.Minesweeper

    529.Minesweeper Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char ma ...