一段时间以来,我一直在 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 -jar
和 mvn spring-boot:run
运行是一样的。我错过了什么?
我想我可以修复循环依赖,但现在困扰我的是为什么这两个运行者不同。
谢谢。
最佳答案
是spring的问题,看
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 - 仅在运行 java -jar 时循环依赖,不使用 spring-boot :run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43255754/