簡體   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