繁体   English   中英

循环依赖只在运行 java -jar 时,而不是 spring-boot:run

[英]Cyclic dependency only when running java -jar, not with spring-boot:run

一段时间以来,我一直在 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是一样的。 我错过了什么?

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

谢谢。

春天有问题,看
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 -jarmvn spring-boot:run runners 不同。 添加@Lazy适用于java -jar runner。

一个.java

@Autowired
@Lazy
//setter injection
public void setB(B b) {
    this.b = b;
}

B.java

// constructor injection
@Autowired
public B(A a) {
  this.a = a;
}

暂无
暂无

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

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