Spring Boot2.6.0新特性之默认禁止循环引用

今天在项目中使用MyBatis的分页插件Pagehelper时遇到一个错误,在这里记录一下

Spring Boot2.6.0新特性之默认禁止循环引用

起因

我在项目中使用了MyBatis的分页插件Pagehelper,在项目启动时,编译器报错,如下:

2022-05-08 22:30:27.463  WARN 5356 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration': Requested bean is currently in creation: Is there an unresolvable circular reference?
2022-05-08 22:30:27.465  INFO 5356 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2022-05-08 22:30:27.472  INFO 5356 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2022-05-08 22:30:27.481 ERROR 5356 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌──->──┐
|  com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration
└──<-──┘


Action:

Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

分析

看到报错我心里是懵批的,因为之前这样写没出现过问题,然后一看报错信息,发现是循环依赖的问题。我翻了翻代码,发现我没有在哪里注入依赖循环引用啊,然后再看仔细一点,前面有一个警告,SpringBoot为Pagehelper生成了Bean,然后产生了依赖循环引用。

遂百度,发现遇到这问题的人还不少,是因为SPringBoot在2.6.0后就默认禁止了依赖循环引用吗,而我这次创建的项目又是用的最新版的SpringBoot版本,自然就踩这个坑了。

解决方法

在application.properties中配置如下:

spring.main.allow-circular-references=true

在application.yml中配置如下:

spring:
  main:
    allow-circular-references: true
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值