仅在运行java -jar时使用循环依赖,而不是使用spring-boot:run
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了仅在运行java -jar时使用循环依赖,而不是使用spring-boot:run,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3154字,纯文字阅读大概需要5分钟。
内容图文
![仅在运行java -jar时使用循环依赖,而不是使用spring-boot:run](/upload/InfoBanner/zyjiaocheng/774/02f49b26969e4da782665bb6b8a86c97.jpg)
我一直在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;
}
内容总结
以上是互联网集市为您收集整理的仅在运行java -jar时使用循环依赖,而不是使用spring-boot:run全部内容,希望文章能够帮你解决仅在运行java -jar时使用循环依赖,而不是使用spring-boot:run所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。