仅在运行java -jar时使用循环依赖,而不是使用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运行它:运行它的工作原理!

我的印象是运行java -jar和mvn spring-boot:run会是一样的.我错过了什么?

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

谢谢.

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

不完全确定为什么java -jar和mvn spring-boot:run runners是不同的.添加@Lazy适用于java -jar runner.

A.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;
}
点赞