前言
因为安全加固需要,要将业务使用的 Spring Boot
框架从 1.5.12.RELEASE
升级到 2.7.0
版本,升级过程中遇到了很多坑,特此记录,供后面遇到的同学参考。
升级方法
找到项目的主 pom.xml
文件,修改其中 spring-boot-starter-parent
组件的版本号:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
</parent>
注意:修改
pom.xml
文件后,记得要更新依赖关系!
改动记录
升级版本后,有很多的报错,经过整理归类,主要包含下列几类错误。
SpringBootServletInitializer 类不存在
新版本的 Spring Boot 中,SpringBootServletInitializer 类的位置发生了变化。
升级前:
import org.springframework.boot.web.support.SpringBootServletInitializer;
升级后:
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
Valid 类不存在
升级前无此报错,升级后 Spring boot 框架没有隐含引入此依赖包,需要人工引入。
参考链接:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes
升级后:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>2.6.7</version>
</dependency>
Logger/LogManage 位置变化(Log4j 升级)
升级前:
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
升级后:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
RelaxedPropertyResolver 类不存在
新版本的 Spring Boot 中,RelaxedPropertyResolver 已经废弃,将采用新方法获取配置文件中的参数。
参考连接:https://www.jianshu.com/p/479c7f60e014
升级前:
@Configuration
public class TestConfig implements EnvironmentAware {
RelaxedPropertyResolver relaxedPropertyResolver ;
TestConfig (ApplicationContext applicationContext , Environment environment){
System.out.println(relaxedPropertyResolver.getProperty("configVersion"))
}
}
升级后:
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
@Configuration
public class TestConfig implements EnvironmentAware {
Binder binder;
TestConfig (ApplicationContext applicationContext , Environment environment){
BindResult<String> bindResult = this.binder.bind("config.version", String.class);
System.out.println(bindResult.get()
}
}
程序包 org.junit
不存在
升级前无此报错,升级后 Spring boot 框架没有隐含引入此依赖包,需要人工引入。
升级后:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
启动 Skipping MapperFactoryBean with name 'aMapper' and 'xx.xx.aMapper' mapperInterface. Bean already defined with the same name 提示
升级后:
去除启动类的@MapperScan注解
运行 NoSuchMethodError 报错
详细错误信息:
NoSuchMethodError: org.springframework.data.redis.core.RedisTemplate.delete(Ljava/lang/Object;)V
此问题是因为spring-boot-starter-data-redis和spring-data-redis版本不兼容导致的,springboot升级到2.6.7后spring-boot-starter-data-redis会跟着升级,导致spring-data-redis老旧。
参考链接:https://blog.csdn.net/A434534658/article/details/118696414
升级前:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.11.RELEASE</version>
</dependency>
升级后:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.6.4</version>
</dependency>
启动 Error:(35, 32) java: 不兼容的类型: java.lang.String无法转换为org.springframework.util.unit.DataSize 错误
新版本中MultipartConfigFactory属性变化。
参考链接:https://blog.csdn.net/weixin_40836984/article/details/107930865。
升级前:
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize("256KB");
factory.setMaxRequestSize("512KB");
return factory.createMultipartConfig();
}
升级后:
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setMaxFileSize(DataSize.of(256, DataUnit.KILOBYTES));
factory.setMaxRequestSize(DataSize.of(512, DataUnit.KILOBYTES));
return factory.createMultipartConfig();
}