简体   繁体   English

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

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

I've been developing a spring-boot application inside Intellij IDEA for a while.一段时间以来,我一直在 Intellij IDEA 中开发一个 spring-boot 应用程序。 It's now complete and I'm about to send it of to other users.现在已经完成,我将把它发送给其他用户。

I build it with mvn clean install and try to start the built -jar file with java -jar target/my-app.jar .我使用mvn clean install构建它并尝试使用java -jar target/my-app.jar启动构建的 -jar 文件。

To my surprise it fails with an exception (hard to read but at least chopped up into several lines)令我惊讶的是它失败了一个例外(难以阅读,但至少被分成几行)

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?

I also get some ascii art over the dependencies我还获得了一些关于依赖项的 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
└─────┘

So I try to run it with mvn spring-boot:run and it works!所以我尝试用mvn spring-boot:run它,它工作正常!

I was under the impression that running with java -jar and mvn spring-boot:run would be the same.我的印象是使用java -jarmvn spring-boot:run是一样的。 What am I missing?我错过了什么?

I guess I could fix the cyclic dependencies but what bothers me now is why these two runners differ.我想我可以修复循环依赖,但现在困扰我的是为什么这两个跑步者不同。

Thanks.谢谢。

It's a problem in spring, see春天有问题,看
https://github.com/spring-projects/spring-boot/issues/6045 https://github.com/spring-projects/spring-boot/issues/6045
https://github.com/spring-projects/spring-framework/issues/18879 https://github.com/spring-projects/spring-framework/issues/18879

Temporary crutch solution临时拐杖解决方案
set in application.properties or different way this:在 application.properties 或不同的方式设置:
spring.main.lazy-initialization=true

[Adding as an answer as couldn't comment] [添加为无法评论的答案]

Not exactly sure why java -jar and mvn spring-boot:run runners are different.不确定为什么java -jarmvn spring-boot:run runners 不同。 Adding @Lazy worked for java -jar runner.添加@Lazy适用于java -jar runner。

A.java一个.java

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

B.java 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