首先,讲一讲我为啥会要更新 spring boot, 项目原先用的是 spring boot 1.5.6 ,相对稳定。 但是因需求需要引入 Mongodb ,而 mongodb 的这一套是基于 spring-data-mongodb 2.x 开发的, 而 spring boot 1.5.6 里直接引入 spring-data-mongodb 2.x 不起作用,因为 spring-data-mongodb 2.x 只有 spring boot 2.x 才支持。
首先,spring 官方有一个升级文档:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide
这里推荐 程序猿DD 做的翻译,写的很全:
http://blog.didispace.com/Spring-Boot-2.0-Migration-Guide/
这两篇文章在我升级的时候都有参考,下面我列出我这次升级踩到的几个坑以及解决方案。
- pom.xml
首先是改 pom 文件:
因为 spring boot 2 对于很多东西的最低版本都有要求,所以需要按需升级,比如我这边就有这个,从1.1升级到了2.1<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> </parent>
<dependency> <groupId>xxxxx</groupId> <artifactId>spring-boot-starter-config-encrypt</artifactId> <version>2.1</version> </dependency>
这里看文档里面有推荐一个临时的模块:
原文:一旦作为该模块作为依赖被添加到你的项目中,它不仅会分析应用程序的环境,而且还会在启动时打印诊断信息,而且还会在运行时为您暂时迁移属性<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-properties-migrator</artifactId> </dependency>
注意:完成迁移后,请确保从项目的依赖关系中删除此模块。 - application.properties 命名规则的更改
这个在升级后,检查一下 application.properties , 命名规则改变的属性会出现删除线,就像server.context-path
这种都需要找到对应的更改。
a.嵌入式容器包装结构
为了支持响应式用例,嵌入式容器包结构已经被大幅度的重构。
EmbeddedServletContainer
已被重新命名为,WebServer
并且该org.springframework.boot.context.embedded
包已被重新定位到org.springframework.boot.web.embedded
。例如,如果您使用TomcatEmbeddedServletContainerFactory
回调接口定制嵌入式 Tomcat 容器,则应该使用TomcatServletWebServerFactory
。特定于 Servlet 的服务器属性
许多
server.*
属性 ( Servlet 特有的) 已经转移到server.servlet
:
b.Spring Boot Actuator旧的属性 新的属性 server.context-parameters.* server.servlet.context-parameters.* server.context-path server.servlet.context-path server.jsp.class-name server.servlet.jsp.class-name server.jsp.init-parameters.* server.servlet.jsp.init-parameters.* server.jsp.registered server.servlet.jsp.registered server.servlet-path server.servlet.path Spring Boot 2 为 Actuator 带来了重要变化,无论是内部还是面向用户,请查阅参考指南中的更新部分和新的Actuator API文档。
您应该期望编程模型,配置密钥和某些端点的响应格式发生变化。Actuator 现在在 Spring MVC,Spring WebFlux 和Jersey 上得到本地支持。
构建
Actuator 的代码分为两个模块:现有的
spring-boot-actuator
和新的spring-boot-actuator-autoconfigure
。如果您使用原始模块(spring-boot-actuator
)导入 actuator,请考虑使用spring-boot-starter-actuator
启动器替代它。Keys 的配置结构
Endpoints 基础配置 key 已经统一:
旧的属性 新的属性 endpoints.<id>.*
management.endpoint.<id>.*
endpoints.cors.*
management.endpoints.web.cors.*
endpoints.jmx.*
management.endpoints.jmx.*
management.address
management.server.address
management.context-path
management.server.servlet.context-path
management.ssl.*
management.server.ssl.*
management.port
management.server.port - 日志类报错:
在改完 pom 和 properties 之后, 因为我原先项目使用的是 log4j ,编译就会报错。
Spring Boot 2.0 默认不包含 log4j,建议使用 slf4j 。
这里为了减少改动量,不推荐把所有的 log4j 都改成 slf4j.
我们直接在 pom 里引入 log4j 即可:<dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
-
SpringApplication 启动类报错 :
Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
如果你遇到了这个问题,就是如果像我一样,不是一个web 项目,那么需要申明,这不是一个 WEB 项目,可以在启动类里面加上一个这个:
ApplicationContext context = new SpringApplicationBuilder() .sources(TestApplication.class) .listeners(new AppListener()) .web(WebApplicationType.NONE) .run(args);
.web(WebApplicationType.NONE)
当然也可能是其他原因,这一篇文章下面的评论有各种情况以及解决方案,大家可以参考一下:
https://stackoverflow.com/questions/21783391/spring-boot-unable-to-start-embeddedwebapplicationcontext-due-to-missing-embedd
如果你的项目虽然不是 web , 但是依然需要 request url 那么加上这个声明 ,会导致这个 call 失败。 - mongodb 的自动配置
这个就是 spring boot 内置了 mongodb 驱动, 会自动读取 mongodb 的配置去连 mongodb , 如果本地没有并不需要连没有配置 mongodb 也会报一个错:
这个解决方案是:
这里需要我们手动的去禁止 mongodb 的自启动:
在启动类加上注解 :
这里需要我们手动去禁用,在启动类加一个注解:
@SpringBootApplication(exclude = MongoAutoConfiguration.class)
- Quartz 集成的改变
之前在 spring boot 1.x 里使用 Quartz, 只需要在方法上加上一个 简单的 @Schedule
spring boot 2.x 里这个注解会不起作用, 需要在 类 上面添加注解 @EnableSchedule
一定要加!