Spring Boot 1.0 升级到 2.0 的时候遇到一些问题
将云收藏从 Spring Boot 1.0 升级到 2.0 的时候也遇到了一些问题,在修改的过程中记录下来,今天整理一下分享出来,方便后续升级的朋友少踩一些坑。
1、第一个问题:启动类报错
Spring Boot 部署到 Tomcat 中去启动时需要在启动类添加SpringBootServletInitializer,2.0 和 1.0 有区别。
// 1.0
import org.springframework.boot.web.support.SpringBootServletInitializer;
// 2.0
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;@SpringBootApplication
public class UserManageApplication extends SpringBootServletInitializer {@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder application) {return application.sources(UserManageApplication.class);}public static void main(String[] args) throws Exception {SpringApplication.run(UserManageApplication.class, args);}
}
这个问题好解决只需要重新导包就行。
2、日志类报错:Spring Boot 2.0 默认不包含 log4j,建议使用 slf4j 。
import org.apache.log4j.Logger;
protected Logger logger = Logger.getLogger(this.getClass());
改为:import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
protected Logger logger = LoggerFactory.getLogger(this.getClass());
这个也比较好改动,就是需要替换的文件比较多。
3、Spring Boot 2.0 去掉了findOne()方法。
以前的findOne()方法其实就是根据传入的 Id 来查找对象,所以在 Spring Boot 2.0 的 Repository 中我们可以添加findById(long id)来替换使用。
例如:
User user=userRepository.findOne(Long id)
改为手动在userRepository手动添加findById(long id)方法,使用时将findOne()调用改为findById(long id)
User user=userRepository.findById(long id)
delete()方法和findOne()类似也被去掉了,可以使用deleteById(Long id)来替换,还有一个不同点是deleteById(Long id)默认实现返回值为void。
Long deleteById(Long id);
改为
//delete 改为 void 类型
void deleteById(Long id);
当然我们还有一种方案可以解决上述的两种变化,就是自定义 Sql,但是没有上述方案简单不建议使用。
@Query("select t from Tag t where t.tagId = :tagId")
Tag getByTagId(@Param("tagId") long tagId);
4、云收藏升级到 2.0 之后,插入数据会报错,错误信息如下:
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
....
Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
...
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '299' for key 'PRIMARY'
这个问题稍稍花费了一点时间,报错提示的是主键冲突,跟踪数据库的数据发现并没有主键冲突,最后才发现是 Spring Boot 2.0 需要指定主键的自增策略,这个和 Spring Boot 1.0 有所区别,1.0 会使用默认的策略。
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private long id;
改动也比较简单,需要在所有的主键上面显示的指明自增策略。
5、Thymeleaf 3.0 默认不包含布局模块。
这个问题比较尴尬,当我将 Pom 包升级到 2.0 之后,访问首页的时候一片空白什么都没有,查看后台也没有任何的报错信息,首先尝试着跟踪了 http 请求,对比了一下也没有发现什么异常,在查询 Thymeleaf 3.0 变化时才发现:Spring Boot 2.0 中spring-boot-starter-thymeleaf 包默认并不包含布局模块,需要使用的时候单独添加,添加布局模块如下:
<dependency><groupId>nz.net.ultraq.thymeleaf</groupId><artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
改完之后再访问首页,一切正常,但是回头查看日志信息发现有一个告警信息:
2
018-05-10 10:47:00.029 WARN 1536 --- [nio-8080-exec-2] n.n.u.t.decorators.DecoratorProcessor : The layout:decorator/data-layout-decorator processor has been deprecated and will be removed in the next major version of the layout dialect. Please use layout:decorate/data-layout-decorate instead to future-proof your code. See https://github.com/ultraq/thymeleaf-layout-dialect/issues/95 for more information.
2018-05-10 10:47:00.154 WARN 1536 --- [nio-8080-exec-2] n.n.u.t.expressions.ExpressionProcessor : Fragment expression "layout" is being wrapped as a Thymeleaf 3 fragment expression (~{...}) for backwards compatibility purposes. This wrapping will be dropped in the next major version of the expression processor, so please rewrite as a Thymeleaf 3 fragment expression to future-proof your code. See https://github.com/thymeleaf/thymeleaf/issues/451 for more information.
跟踪地址看了一下,大概的意思是以前布局的标签已经过期了,推荐使用新的标签来进行页面布局,解决方式也比较简单,修改以前的布局标签 layout:decorator 为 layout:decorate即可。
6、分页组件PageRequest变化。
在 Spring Boot 2.0 中 ,方法new PageRequest(page, size, sort) 已经过期不再推荐使用,推荐使用以下方式来构建分页信息:
Pageable pageable =PageRequest.of(page, size, Sort.by(Sort.Direction.ASC,"id"));
跟踪了一下源码发现PageRequest.of()方法,内部还是使用的new PageRequest(page, size, sort),只是最新的写法更简洁一些。
public static PageRequest of(int page, int size, Sort sort) {return new PageRequest(page, size, sort);
}
7、关联查询时候组合返回对象的默认值有变化。
在使用 Spring Boot 1.0 时,使用 Jpa 关联查询时我们会构建一个接口对象来接收结果集,类似如下:
public interface CollectView{Long getId();Long getUserId();String getProfilePicture();String getTitle();
}
在使用 Spring Boot 1.0 时,如果没有查询到对应的字段会返回空,在 Spring Boot 2.0 中会直接报空指针异常,对结果集的检查会更加严格一些。
8、其它优化
前段时间在学习 Docker ,给云收藏添加了 Docker 、Docker Compose 支持让部署的时候更简单一些;同时修复了一些 bug,对于明显很消耗资源的功能进行了改进,部分功能添加了容错性;本次部署的时候使用了 Nginx 作为反向代理,因为使用了 WebJars 暂时不能使用 Nginx 代理 Js,所以将除过 Js 以外的其它资源都配置了缓存,;数据库由 Mysql 换成了 Mariadb。
以上就是云收藏从 Spring Boot 1.0 到 2.0 所做的一些小改进,做完这些工作之后惊喜的发现云收藏的访问速度比以前快了很多,虽然还有很大的优化空间,但日常使用基本上不会体验到太大的延迟。Spring Boot 2.0 中 Thymeleaf 默认使用了 3.0 ,数据库连接池默认使用了 Hikari ,这两个组件在性能上有很大的提升,同时也是提升云收藏访问速度的因素之一。
Spring Boot 1.0 升级到 2.0 的时候遇到一些问题相关推荐
- Spring Boot 从1.0 升级到 2.0 所踩的坑
先给大家晒一下云收藏的几个数据,作为一个 Spring Boot 的开源项目(https://github.com/cloudfavorites/favorites-web)目前在 Github 上面 ...
- 记selenium1.0升级到selenium2.0
前阵子因为要进行支持多浏览器的自动化测试,原来selenium1.0仅支持到firefox3.6,IE8,chrome4的版本,而公司GA数据显示用户多使用IE9,Firefox20,chrome26 ...
- WSS2.0升级到WSS3.0
WSS2.0升级到WSS3.0的过程: 1.检查服务器上面是否安装.Net Framework3.0(必须要安装的). 2.安装WSS3.0(安装后别做配置) 选择第一项(逐步升级),默认的是第二项( ...
- Oracle10.2.0.1.0升级Oracle10.2.0.2.0补丁安装指南(转载)
Oracle10.2.0.1.0升级Oracle10.2.0.2.0补丁安装指南 Oracle数据库学习之路 2008-07-25 11:35:12 阅读5 评论0 字号:大中小 一.安装数据库 先安 ...
- Finereport 9.0升级到10.0工具下载[9-10升级工具]
QQ192374926 1. 描述 现提供一个独立的辅助升级程序,可以将 9.0 的 Web 工程升级到 10.0 工程,包括 JAR 包.各类配置文件.FineDB 数据表等的兼容升级. 注:支持 ...
- 初次Android 6.0升级Android 8.0的心酸总结
前置条件 1.需要先确定你要升级的Android版本(这里我是从6.0升级到8.0,也就是compileSdkVersion从23升级到26),无论是升级到什么版本,你的AS都至少需要升级到3.0以上 ...
- Spring Boot2.x-10 基于Spring Boot 2.1.2 + Mybatis 2.0.0实现多数据源,支持事务
文章目录 概述 思路 步骤 Step1 多数据源配置文件applicaiton.yml Step2 初始化多个数据源 Step3 配置多个数据源 验证测试 支持事务 Step1 配置类中通过@Bean ...
- 127.0.0.1 myz.php,附录 – 1.0升级到2.0注意事项 - GatewayWorker 2.x 3.x 手册
如何知道自己使用的版本是1.0还是2.0 打开文件 GatewayWorker/Gateway.php,查看是否有常量VERSION,并且常量值大于等于2.0.0 class Gateway exte ...
- web3.0 升级到webpack5.0 以及兼容IE处理
如果你的事vue-cli 或者其他cli 关于IE 兼容不具有什么参考价值.如果是react 项目,你看看就行. webpack3的打包速度已成为诟病,所以我们将原来的webpack3升级至webp ...
最新文章
- linux shell编程时报:bad substitution错误的解决办法
- 迈向智慧化 物联网规模应用不断拓展
- 普通类可以抛出异常吗
- 从原理上搞懂如何设置线程池参数大小?
- 前端请求,后台http 连接输出流交给ServeletResponse 下载文件
- linux新建好文件后怎么编译,使用autoconf生成Makefile并编译工程的步骤
- 《Python Cookbook 3rd》笔记(4.16):迭代器代替 while 无限循环
- 开源项目征集 | CSDN “开源加速器计划”之【开源技术栈选型 Show】
- zabbix 4.0 安装配置
- Linux忘记开机密码
- 细说背包问题 - 完全背包
- JustTrustMe 原理分析
- vscode快速删除空白行方法
- 视频信息和信号的特点
- 利用comtypes.client批处理dwg文件(AutoCAD二次开发Python)
- QScrollArea手指触摸屏滑动
- 好用的待办事项APP有哪些
- ecef与enu的转换
- Git_GitHub——基本操作、创建远程库、远程库操作、团队协作、SSH免密登录
- 齐博x1教程:快速增加下拉菜单