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

时间:2017-04-06 12:50:39

标签: java spring spring-boot

我一直在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一起运行会是一样的。我错过了什么?

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

感谢。

2 个答案:

答案 0 :(得分:1)

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

不确定actor: UnitTester modules: enabled: - Asserts - Laravel5: part: ORM - \Helper\Unit java -jar的跑步者为何不同。添加mvn spring-boot:run对于@Lazy的跑步者是有用的。

A.java

java -jar

B.java

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

答案 1 :(得分:0)

春天是个问题,请参见
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