有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Gradle构建的Java Spring Boot项目中偶尔出现BeanCreationException

有时,当启动由Gradle构建的JavaSpring引导单JAR应用程序时,我会遇到BeanCreationException。我的调查表明,这取决于JAR文件以及Gradle是如何构建的。在Linux系统上构建项目时,我经常(但并非总是)看到异常。但我从未在Windows上构建JAR或从IntelliJ Idea运行时再现过这个问题

我试图将一个工作的JAR的内容与抛出异常的JAR进行比较——所有的*。类文件(包括meta和resources)是二进制相等的,唯一的区别在于文件存储在JAR/ZIP归档文件中的顺序。我还尝试在Windows上解包失败的JAR,并将其重新打包到一个新的JAR文件中(使用7-zip)——应用程序启动时没有任何异常。这个奇怪的解决方法解决了这个问题,但我不想在linux机器上每次构建之后都这样做

异常建议检查循环引用,因此我尝试用@autowiredbean构造函数替换@Autowired属性,以帮助我找到问题,但没有帮助。stacktrace没有提到我的任何类,所以我不知道是什么bean导致了这个问题。由于这个问题只是偶尔发生,并且可以通过重新打包JAR文件来解决,所以我不确定是否存在任何循环引用

你能帮帮我吗?欢迎任何意见或建议

JDK:openjdk8u262 渐变版本5.6 弹簧靴2.3.1

异常消息:

BeanCreationException: Error creating bean with name 'webConfig':
Invocation of init method failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource
[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: 
Unsatisfied dependency expressed through method 'requestMappingHandlerAdapter' parameter 0;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'mvcContentNegotiationManager':
Requested bean is currently in creation: Is there an unresolvable circular reference?

共 (1) 个答案

  1. # 1 楼答案

    默认情况下,Spring自行管理bean的生命周期,然后在启动期间安排它们的初始化顺序

    下面是使用@DependsOn注释对bean实例进行排序的完整示例

    您还可以在官方Spring documentation中找到更多详细信息