Spring Boot3.0升级,踩坑之旅,附解决方案

一. 编译报错,import javax.servlet.*; 不存在

这个报错主要是Spring Boot3.0已经为所有依赖项从 Java EE 迁移到 Jakarta EE API,导致 servlet 包名的修改,Spring团队这样做的原因,主要是避免 Oracle 的版权问题,解决办法很简单,两步走:

1 添加 jakarta.servlet 依赖

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
</dependency>


  1. 修改项目内所有代码的导入依赖
修改前:
import javax.servlet.*
修改后:
import jakarta.servlet.*


二. 附带的众多依赖包升级,导致的部分代码写法过期报警

2.1 Thymeleaf升级到3.1.0.M2,日志打印的报警

14:40:39.936 [http-nio-84-exec-15] WARN  o.t.s.p.StandardIncludeTagProcessor - [doProcess,67] - [THYMELEAF][http-nio-84-exec-15][admin/goods/goods] Deprecated attribute {th:include,data-th-include} found in template admin/goods/goods, line 4, col 15\. Please use {th:insert,data-th-insert} instead, this deprecated attribute will be removed in future versions of Thymeleaf.
14:40:39.936 [http-nio-84-exec-15] WARN  o.t.s.p.AbstractStandardFragmentInsertionTagProcessor - [computeFragment,385] - [THYMELEAF][http-nio-84-exec-15][admin/goods/goods] Deprecated unwrapped fragment expression "admin/header :: header-fragment" found in template admin/goods/goods, line 4, col 15\. Please use the complete syntax of fragment expressions instead ("~{admin/header :: header-fragment}"). The old, unwrapped syntax for fragment expressions will be removed in future versions of Thymeleaf.


可以看出作者很贴心,日志里已经给出了升级后的写法,修改如下:

修改前:
<th:block th:include="admin/header :: header-fragment"/>
修改后:
<th:block th:insert="~{admin/header :: header-fragment}"/>


2.2 Thymeleaf升级到3.1.0.M2,后端使用 thymeleafViewResolver 手动渲染网页代码报错

// 修改前 Spring Boot2.7:
WebContext ctx = new (request, response,
        request.getServletContext(), request.getLocale(), model.asMap());
html = thymeleafViewResolver.getTemplateEngine().process("mall/seckill-list", ctx);


上述代码中针对 WebContext 对象的创建报错,这里直接给出新版写法

// 修改后 Spring Boot3.0:
JakartaServletWebApplication jakartaServletWebApplication = JakartaServletWebApplication.buildApplication(request.getServletContext());
WebContext ctx = new WebContext(jakartaServletWebApplication.buildExchange(request, response), request.getLocale(), model.asMap());
html = thymeleafViewResolver.getTemplateEngine().process("mall/seckill-list", ctx);


三. 大量第三方库关于 Spring Boot 的 starter 依赖失效,导致项目启动报错

博主升级到3.0后,发现启动时,Druid 数据源开始报错,找不到数据源配置,便怀疑跟 Spring boot 3.0 更新有关

这里直接给出原因:Spring Boot 3.0 中自动配置注册的 spring.factories 写法已废弃,改为了 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 写法,导致大量第三方 starter 依赖失效

在吐槽一下,这么重要的更改在Spring官方的 Spring-Boot-3.0-发布说明 中竟然没有,被放在了 Spring-Boot-3.0.0-M5-发布说明 中

这里给出两个解决方案:

  1. 等待第三方库适配 Spring Boot 3.0
  2. 按照 Spring Boot 3.0要求,在项目resources 下新建 META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports 文件,手动将第三方库的 spring.factories 加到 imports 中,这样可以手动修复第三方库 spring boot starter 依赖失效问题

四. Mybatis Plus 依赖问题

Mybatis plus最新版本还是3.5.2,其依赖的mybatis-spring版本是2.2.2(mybatis-spring已经发布了3.0.0版本适配Spring Boot3.0),这会导致项目中的sql查询直接报错,这里主要是因Spring Boot3.0中删除NestedIOException这个类,在Spring boot2.7中这个类还存在,给出类说明截图

这个类在2.7中已经被标记为废弃,建议替换为IOException, 而Mybatis plus3.5.2中还在使用。这里给出问题截图MybatisSqlSessionFactoryBean这个类还在使用NestedIOException

查看 Mybatis plus 官方issue也已经有人提到了这个问题,官方的说法是 mybatis-plus-spring-boot-starter 还在验证尚未推送maven官方仓库,这里我就不得不动用我的小聪明,给出解决方案:

  1. 手动将原有的 MybatisSqlSessionFactoryBean 类代码复制到一个我们自己代码目录下新建的 MybatisSqlSessionFactoryBean 类,去掉 NestedIOException 依赖
  2. 数据源自动配置代码修改
@Slf4j
@EnableConfigurationProperties(MybatisPlusProperties.class)
@EnableTransactionManagement
@EnableAspectJAutoProxy
@Configuration
@MapperScan(basePackages = "ltd.newbee.mall.core.dao", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class HikariCpConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean(name = "masterDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDataSource() {
        return new HikariDataSource();
    }

    /**
     * @param datasource 数据源
     * @return SqlSessionFactory
     * @Primary 默认SqlSessionFactory
     */
    @Bean(name = "masterSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource datasource,
                                                     Interceptor interceptor,
                                                     MybatisPlusProperties properties) throws Exception {
        MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
        bean.setDataSource(datasource);
        // 兼容mybatis plus的自动配置写法
        bean.setMapperLocations(properties.resolveMapperLocations());
        if (properties.getConfigurationProperties() != null) {
            bean.setConfigurationProperties(properties.getConfigurationProperties());
        }
        if (StringUtils.hasLength(properties.getTypeAliasesPackage())) {
            bean.setTypeAliasesPackage(properties.getTypeAliasesPackage());
        }
        bean.setPlugins(interceptor);
        GlobalConfig globalConfig = properties.getGlobalConfig();
        bean.setGlobalConfig(globalConfig);
        log.info("------------------------------------------masterDataSource 配置成功");
        return bean.getObject();
    }

    @Bean("masterSessionTemplate")
    public SqlSessionTemplate masterSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

}


到这里,项目就能够正常跑起来了

总结

Spring Boot 3.0 升级带来了很多破坏性更改,把众多依赖升级到了最新,算是解决了一部分历史问题,也为了云原型需求,逐步适配 graalvm ,不管怎么样作为技术开发者,希望有更多的开发者来尝试 Spring Boot 3.0 带来的新变化。

#java后端##程序员#
全部评论

相关推荐

2025-11-08 21:43
济南大学 Java
1.3000~4000,一般对于双非本来说,hr能给出这种薪资,完完全全是侮辱人,而且我感觉是那种毛病贼多的小公司,遇上脾气不好的,能直接开骂,3-4k真的能招到人吗,属于是拉完了。2.4000~6000,可能是考研失败或者是hr看着是学校确实没那么好给出的价,如果是考研失败想找个地方过度一下准备二战的话,可以去一下,毕竟确实给的不多,校招都给这么点了,长期干的话薪资也不会涨到哪里去,这里给到npc。3.6000~8000,取中位数7k来看,除了某些提前规划大学生活,积累实习进入大厂的,我感觉这是大部分双非本能够拿到的工资,因为不管是boss上还是其他渠道,感觉普通后端开发都是这个数(以山东济南这边的情况来看),如果能双休而且是对口工作的话,这个薪资范围对双非本来说还是可以的,这里给到人上人。4.8000~10000,这个区间来看,感觉超越80%的双非本的同学了,如果你是9k而且双休,那简直是太舒服了,毕竟学历摆在那里,能拿到这个数,已经是“空调wifi冰镇西瓜”的级别了,这里必须夯爆了。5.10~15k,周围的同学极少能拿到这个数,甚至除了进大厂的同学们,很少看到双非本能拿到这个数,当然我指的是普通双非,但是能拿这个数,加班也少不了,而且如果能拿到这个数(相较于9k),那加班我感觉是少不了的,需要天天加班好几个小时,甚至双休都不能保证,但是能拿到这些数了,加会儿班就无可厚非了。第二点,如果能拿到这个薪资,那么这个同学肯定是在学习路上付出了很多的努力来弥补学历上带来的不足。在大学期间需要马不停蹄的来学习,如果也要严重加班的话,那综合来看,我会给到顶级。最后,目前我也是一名应届生,上述也是以山东目前的情况来说(如果是北上广深这样的城市那需要另外斟酌),以自己找工作的经历以及互联网上观察到的双非本的同学的秋招情况来看的,目前也还有好多的同学没有拿到心仪的offer,祝愿同学们拿到自己心仪的工作!!
一天代码十万三:都互联网了,就别看山东情况了
点赞 评论 收藏
分享
2025-11-15 14:35
南京邮电大学 Java
程序员牛肉:你这简历有啥值得拷打的?在牛客你这种简历一抓一大把,也就是个人信息不一样而已。 关键要去找亮点,亮点啊,整个简历都跟流水线生产出来的一样。
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务