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

And*_*and 8 java spring spring-boot

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

我构建它mvn clean install并尝试启动构建的 -jar 文件java -jar target/my-app.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?
Run Code Online (Sandbox Code Playgroud)

我还获得了一些关于依赖项的 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
???????
Run Code Online (Sandbox Code Playgroud)

所以我尝试运行它 mvn spring-boot:run并且它有效!

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

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

谢谢。

小智 1

[添加为无法评论的答案]

不太清楚为什么java -jar跑步mvn spring-boot:run者会有所不同。添加@Lazy对跑步者有用java -jar

A.java

@Autowired
@Lazy
//setter injection
public void setB(B b) {
    this.b = b;
}
Run Code Online (Sandbox Code Playgroud)

java

// constructor injection
@Autowired
public B(A a) {
  this.a = a;
}
Run Code Online (Sandbox Code Playgroud)