今天在项目中使用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
|