Spring boot 从 1.x 升级到 2.x 排雷指南

首先,讲一讲我为啥会要更新 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/

 这两篇文章在我升级的时候都有参考,下面我列出我这次升级踩到的几个坑以及解决方案。       

 

 

  1. pom.xml

    首先是改 pom 文件:
    <parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>2.1.5.RELEASE</version>
    	</parent>
    因为 spring boot 2 对于很多东西的最低版本都有要求,所以需要按需升级,比如我这边就有这个,从1.1升级到了2.1
    <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>
    原文:一旦作为该模块作为依赖被添加到你的项目中,它不仅会分析应用程序的环境,而且还会在启动时打印诊断信息,而且还会在运行时为您暂时迁移属性
    注意:完成迁移后,请确保从项目的依赖关系中删除此模块。
  2. 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

    旧的属性新的属性
    server.context-parameters.*server.servlet.context-parameters.*
    server.context-pathserver.servlet.context-path
    server.jsp.class-nameserver.servlet.jsp.class-name
    server.jsp.init-parameters.*server.servlet.jsp.init-parameters.*
    server.jsp.registeredserver.servlet.jsp.registered
    server.servlet-pathserver.servlet.path
    b.Spring Boot Actuator

    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.addressmanagement.server.address
    management.context-pathmanagement.server.servlet.context-path
    management.ssl.*management.server.ssl.*
    management.portmanagement.server.port
  3. 日志类报错:

    在改完 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>

     

  4. 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 失败。

  5. mongodb 的自动配置

    这个就是 spring boot 内置了 mongodb 驱动, 会自动读取 mongodb 的配置去连 mongodb , 如果本地没有并不需要连没有配置 mongodb 也会报一个错:

      这个解决方案是:
        

          这里需要我们手动的去禁止 mongodb 的自启动:
          在启动类加上注解 : 

        这里需要我们手动去禁用,在启动类加一个注解:
         @SpringBootApplication(exclude = MongoAutoConfiguration.class) 

 

  1. Quartz 集成的改变

    之前在 spring boot 1.x 里使用 Quartz, 只需要在方法上加上一个 简单的 @Schedule 
    spring boot 2.x 里这个注解会不起作用, 需要在 类 上面添加注解 @EnableSchedule
    一定要加!
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值