jdk8升级17后解决的问题
问题1:idea运行报错:Unsupported class file major version 61
问题分析:该报错是jdk17的版本号不支持,降低jdk版本号或升级spinrgboot相关依赖,这里升级springboot为
依赖更新
jdk8 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> </parent> jdk17 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.2</version> <relativePath/> </parent>
问题2:javax包找不到
问题分析:jdk8之后javax替换为了jakarta,idea全局替换
替换后保留jdk17存在的javax例如:
import javax.sql.DataSource; import javax.crypto.*;
问题3:shiro版本问题,导致引用的依赖中还有使用javax包
问题分析:升级shiro版本并排除使用javax相关的包
jdk8 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <shiro.version>1.10.0</shiro.version> </properties> <!-- shiro 用户验证 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>${shiro.version}</version> </dependency> jdk17 <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>17</java.version> <shiro.version>1.11.0</shiro.version> </properties> <!-- shiro 用户验证 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <classifier>jakarta</classifier> <version>${shiro.version}</version> <!-- 排除仍使用了javax.servLet的依赖 --> <exclusions> <exclusion> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> </exclusion> <exclusion> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> </exclusion> </exclusions> </dependency> <!-- shiro 用户验证 引入适配的包 --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <classifier>jakarta</classifier> <version>${shiro.version}</version> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-web</artifactId> <classifier>jakarta</classifier> <version>${shiro.version}</version> <!-- 排除仍使用了javax.servLet的依赖 --> <exclusions> <exclusion> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> </exclusion> </exclusions> </dependency>
问题4:lombok版本升级
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <scope>provided</scope> <version>1.18.30</version> </dependency>
问题5:mysql连接器升级
问题分析:这里jdbc需要对应:
driver-class-name: com.mysql.cj.jdbc.Driver
<!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency>
问题6:不兼容的类型: org.apache.http.impl.client.CloseableHttpClient无法转换为org.apache.hc.client5.http.classic.HttpClient
问题分析:升级springboot版本导致的HttpComponentsClientHttpRequestFactory引用的依赖包中.class文件中找不到对应包,这里是版本问题刚好项目中这个方法废弃掉了,直接注释了,不用升级了
把这个类注释掉后,报错:required a bean of type 'org.springframework.web.client.RestTemplate' that could not be found.
保留一个即可,需要代理单独找对应的适配
@Bean(name = "restTemplate") public RestTemplate restTemplate(){ return new RestTemplate(); }
问题7:actory method 'sqlSessionFactory1' threw exception with message: org/springframework/core/NestedIOException
问题分析:无法创建Bean等,springboot升级3.x后,mybatis需要升级
jdk8 <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> jdk17 <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.0</version> </dependency>
问题8:运行找不到符号很多类里类似于user.getId();报错
问题分析:jdk17依赖没加载完成,确认配置jdk正确并清除缓存重启idea
问题9:As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.
问题分析:springboot升级后导致的循环依赖问题;项目中默认禁止的循环依赖,也是不鼓励的,通过设置可以打破循环;
main: allow-circular-references: true
安装成功、打包成功、启动成功~
问题10:本地连接本地redis指定database:2,但数据存在了0里;测试环境连接测试redis被拒绝,并且日志中显示localhost:<un..>
问题分析:springboot升级3.x后对应redis也升级了连接配置需要变更,redis没指定版本会跟着springboot走
依赖变更
springboot2.x <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.1</version> <relativePath/> </parent> <!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> springboot3.x <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.1.2</version> <relativePath/> </parent> <!--redis--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session-data-redis</artifactId> </dependency>
配置变更
springboot2.x redis: host: localhost port: 6379 password: 123456 #连接超时时间(毫秒) timeout: 3000 #redis数据库索引(默认为0) database: 2 jedis: pool: #连接池最大连接数(负值表示没有限制) max-active: 2000 #连接池中的最大空闲连接 max-idle: 50 #连接池中的最小空闲连接 min-idle: 5 #连接池最大阻塞等待时间(负值表示没有限制) max-wait: 1000 springboot3.x data: redis: host: localhost port: 6379 password: 123456 timeout: 3000 database: 2 lettuce: pool: max-active: 2000 max-idle: 50 min-idle: 5 max-wait: 1000
代码变更
springboot2.x import org.springframework.data.redis.connection.RedisConnectionFactory; @Bean("redisTemplate") public RedisTemplate<String, Object> getRedisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();//序列化类型(只能序列化字符串类型的数据,不能序列化Bean) FastJsonRedisSerializer<Object> fastJsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);//在redis可视化工具里可以json的形式查看序列化结果 redisTemplate.setConnectionFactory(factory); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setValueSerializer(fastJsonRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); redisTemplate.setHashValueSerializer(fastJsonRedisSerializer); return redisTemplate; } @Bean("redisStringTemplate") public RedisTemplate<String, String> getRedisStringTemplate(RedisConnectionFactory factory) { RedisTemplate<String, String> redisStringTemplate = new RedisTemplate<String, String>(); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();//序列化类型(只能序列化字符串类型的数据,不能序列化Bean) redisStringTemplate.setConnectionFactory(factory); redisStringTemplate.setKeySerializer(stringRedisSerializer); redisStringTemplate.setValueSerializer(stringRedisSerializer); redisStringTemplate.setHashKeySerializer(stringRedisSerializer); redisStringTemplate.setHashValueSerializer(stringRedisSerializer); return redisStringTemplate; } @Bean("redisSessionTemplate") public RedisTemplate<String, Object> getRedisSessionTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); redisTemplate.setConnectionFactory(factory); redisTemplate.setKeySerializer(new StringRedisSerializer()); // key的序列化类型 redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer()); // value的序列化类型(Bean必须实现Serializable接口,序列化后的结果非常庞大,大概是json格式的5倍)(默认为JdkSerializationRedisSerializer) return redisTemplate; } springboot3.x import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; public RedisTemplate<String, Object> getRedisTemplate(LettuceConnectionFactory factory) {} public RedisTemplate<String, String> getRedisStringTemplate(LettuceConnectionFactory factory) {} public RedisTemplate<String, Object> getRedisSessionTemplate(LettuceConnectionFactory factory) {}