maven循環依賴,解決springboot 循環依賴

 2023-10-11 阅读 33 评论 0

摘要:錯誤提示 Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.mai

錯誤提示
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

Despite circular references being allowed, the dependency cycle between beans could not be broken. Update your application to remove the dependency cycle.

  • 問題是兩個類互相調用,引發了循環依賴

解決方法:

  • 簡單方式

在注入的時候加上注解 @Lazy

@Lazy
EmployeeManagementService employeeManagementService;

如果注解不行:

  • 暴力方式,在配置文件中,添加
spring:main:allow-circular-references: true
  • 如果暴力方法沒有解決,可以通過反射機制來解決:
  EmployeeManagementService employeeManagementService;//解決循環依賴public EmployeeManagementService getEmployeeManagementService(){this.employeeManagementService=SpringUtil.getBean(EmployeeManagementService.class);return employeeManagementService;}

maven循環依賴,SpringUtil可以用hutool的

<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.4.2</version></dependency>

也可以用xin.altitude.cms.common的SpringUtils

	<dependency><groupId>xin.altitude.cms.common</groupId><artifactId>ucode-cms-common</artifactId><version>1.3.4</version></dependency>

如果找不到這個依賴可以添加

    <repositories><repository><id>public</id><name>maven nexus</name><url>https://repo1.maven.org/maven2/</url><snapshots><updatePolicy>always</updatePolicy></snapshots></repository></repositories>

參考網址:https://www.jb51.net/article/238474.htm

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/141094.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息