
一、SpringBootServletInitializer 类所在包的变化
当 SpringBoot 准备打成 war 包部署时,我们需要继承 SpringBootServletInitializer 类。
在 SpringBoot1.5 版本中,SpringBootServletInitializer 类在 org.springframework.boot.web.support 包,而到了 SpringBoot2.2 版本,SpringBootServletInitializer 类迁移到了 org.springframework.boot.web.servlet.support 包。
二、Context-Path 配置项变化
1.5 版本时,配置项目路径使用:server.context-path=/xxx
,而 2.2 版本改成了:server.servlet.context-path=/xxx
三、配置项不能再同驼峰名称需改成横线隔开方式
项目中经常使用 @ConfigurationProperties 注解的类来接收配置,这样 SpringBoot 会自动将配置值注入到类属性中。
在 1.5 版本时,只要配置项对上就可以,如:
1 | # application.properties |
1 | // DataSourcePropConfig.java |
而在 SpringBoot2.2 版本中,这样启动则会报错:
1 | Description: |
意思就是需要用横线分割的方式定义配置项。解决办法就是改用 @Value 方式注入配置:
1 | // DataSourcePropConfig.java |
四、WebMvcConfigurerAdapter 被废弃
在 1.5 版本时,项目做一些个性化配置(比如:addInterceptors、addCorsMappings、addResourceHandlers 等)可以自定义一个类并继承 WebMvcConfigurerAdapter。但在 2.2 版本中该类被废弃了,因为 SpringBoot2.2 要求至少使用 JDK8,WebMvcConfigurerAdapter 抽象类实现的接口 WebMvcConfigurer 里的方法都已经加上了 default 实现,所以在 2.2 版本中,自定义的类直接实现 WebMvcConfigurer 接口就可以了。
1 | // 1.5 |
五、添加 JNDI 数据源的区别
在 1.5 版本中使用 TomcatEmbeddedServletContainerFactory 添加 JNDI 数据源,2.2 版本中 TomcatEmbeddedServletContainerFactory 类不存在了,改成使用 ServletWebServerFactory。
1 | // JNDI 数据源 |