简体   繁体   English

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

[英]Occasional BeanCreationException in Java Spring Boot project built with Gradle

Sometimes I get a BeanCreationException when starting a Java Spring Boot single-JAR application built by Gradle.有时我在启动由 Gradle 构建的 Java Spring Boot 单 JAR 应用程序时会收到 BeanCreationException。 My investigations show that it depends on the JAR file and how it has been built by Gradle.我的调查表明它取决于 JAR 文件以及它是如何由 Gradle 构建的。 Most often (but not always) I see the exception when I build the project on a Linux system.大多数情况下(但并非总是如此),当我在 Linux 系统上构建项目时,我会看到异常。 But I have never reproduced the issue with a JAR built on Windows or when running from IntelliJ Idea.但是我从来没有用在 Windows 上构建的 JAR 或从 IntelliJ Idea 运行时重现这个问题。

I tried to compare the content of a working JAR with the JAR throwing the exception - all the *.class files (including meta and resources) were binary equal, the only difference was in the order the files were stored in the JAR/ZIP archive.我尝试将工作 JAR 的内容与抛出异常的 JAR 的内容进行比较 - 所有 *.class 文件(包括元和资源)都是二进制相等的,唯一的区别在于文件在 JAR/ZIP 存档中的存储顺序. I also tried to unpack the failing JAR on Windows and just repack it into a new JAR file (using 7-zip) - the application started without any exceptions.我还尝试在 Windows 上解压缩失败的 JAR,然后将其重新打包到一个新的 JAR 文件中(使用 7-zip) - 应用程序启动时没有任何异常。 This weird workaround solved the issue, but it's not something I'd like to do after each build on a linux machine.这个奇怪的解决方法解决了这个问题,但这不是我每次在 linux 机器上构建后都想做的事情。

The exception suggests to check circular references, so I tried replacing @Autowired properties with @Autowired bean constructors to help me to find the problem but that didn't help.异常建议检查循环引用,所以我尝试用 @Autowired bean 构造函数替换 @Autowired 属性来帮助我找到问题,但这没有帮助。 The stacktrace does not mention any of my classes, so I don't know what bean could be responsible for the issue.堆栈跟踪没有提到我的任何类,所以我不知道哪个 bean 可能对这个问题负责。 And because of the fact the issue happens only sometimes and is solvable by repacking the JAR file, I'm not sure there are any circular references anyway.而且由于该问题仅偶尔发生并且可以通过重新打包 JAR 文件来解决,因此我不确定是否存在任何循环引用。

Could you please help me?请你帮助我好吗? Any advice or suggestion is welcomed.欢迎任何意见或建议。

JDK: openjdk 8u262 Gradle version 5.6 Spring boot 2.3.1 JDK:openjdk 8u262 Gradle 版本 5.6 Spring boot 2.3.1

Exception message:异常信息:

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?

By default, Spring manages itself bean's lifecycle and then, arranges their initialization order during the startup.默认情况下,Spring 管理自己 bean 的生命周期,然后在启动期间安排它们的初始化顺序。

Here a full example of usage @DependsOn annotation to order the bean instanciation.这里是使用@DependsOn 注释对bean实例化进行排序的完整示例。

You can also found more detail in the official Spring documentation您还可以在官方Spring 文档中找到更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM