java - 仅在运行 java -jar 时循环依赖,不使用 spring-boot :run

标签 java spring spring-boot

一段时间以来,我一直在 Intellij IDEA 中开发 spring-boot 应用程序。它现已完成,我正准备将其发送给其他用户。

我使用 mvn clean install 构建它并尝试使用 java -jar target/my-app.jar 启动构建的 -jar 文件。

令我惊讶的是它失败并出现异常(难以阅读但至少被分成几行)

Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userController':
Unsatisfied dependency expressed through field 'userClient';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '***.client.userclient.UserClient':
FactoryBean threw exception on object creation;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration':
Unsatisfied dependency expressed through method 'setConfigurers' parameter 0;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'webMvcConfig':
Unsatisfied dependency expressed through field 'authenticationInterceptor';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'authenticationInterceptor':
Unsatisfied dependency expressed through field 'authenticationClient';
nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name '***.client.authenticationclient.AuthenticationClient':
FactoryBean threw exception on object creation;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'mvcResourceUrlProvider':
Requested bean is currently in creation: Is there an unresolvable circular reference?

我还得到了一些关于依赖项的 ascii 艺术

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

Description:

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

|  userController (field ****.client.userclient.UserClient ****.controller.user.UserController.userClient)
↑     ↓
|  ****.client.userclient.UserClient
↑     ↓
|  org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$EnableWebMvcConfiguration
↑     ↓
|  webMvcConfig (field ****.AuthenticationInterceptor ****.WebMvcConfig.authenticationInterceptor)
↑     ↓
|  authenticationInterceptor (field ****.client.authenticationclient.AuthenticationClient ****.AuthenticationInterceptor.authenticationClient)
↑     ↓
|  ****.client.authenticationclient.AuthenticationClient
↑     ↓
|  mvcResourceUrlProvider
└─────┘

所以我尝试使用 mvn spring-boot:run 运行它,它成功了!

我的印象是使用 java -jarmvn spring-boot:run 运行是一样的。我错过了什么?

我想我可以修复循环依赖,但现在困扰我的是为什么这两个运行者不同。

谢谢。

最佳答案

是spring的问题,看
https://github.com/spring-projects/spring-boot/issues/6045
https://github.com/spring-projects/spring-framework/issues/18879

临时拐杖解决方案
在 application.properties 或其他方式中设置:
spring.main.lazy-initialization=true

关于java - 仅在运行 java -jar 时循环依赖,不使用 spring-boot :run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43255754/

相关文章:

java - 使用 Spring 3 生成在 Postgres 中使用游标的 sql

java - 当我运行 Spring Batch 作业时 chunkListener 不运行

spring-boot - --spring.output.ansi.enabled 在哪里配置?

java - Spring AMQP客户端自动重新连接但不消耗恢复

java - 如何在 Java 中漂亮地打印 3D 数组

java - 如何在大列表<String>中搜索子字符串 - Java

java - Spring Boot使用@Value进行注入(inject)失败

java - 在 Java 中按键时暂停/取消暂停线程

java - 如何在 PHP 或 Java 中打印十六进制数

javascript - 语义 UI 搜索栏 - 你能切断描述,让它只显示前几个词,但仍然搜索所有词吗?