Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem after circular dependency is disabled by default in SpringBoot2.6.x

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to solve the problem after SpringBoot2.6.x forbids circular dependence by default, it has certain reference value, interested friends can refer to it, I hope you can learn a lot after reading this article, let the editor take you to know it.

I. Preface

SpringBoot 2.6.x does not recommend circular dependency, which is good news. SpringBoot gradually guides developers to write canonical code from the bottom, and it is also sad news that the application scenarios of circular dependence are too extensive.

If you upgrade from a lower version to 2.6.x, there is a good chance that the first problem you encounter is circular dependency.

2. Problem recovery 1. Code description

The following style of code is common: both classes have the need to call each other's methods, so they can easily be written as circular references.

@ Servicepublic class TbDeptServiceImpl extends ServiceImpl implements ITbDeptService {@ Autowired private ITbStaffService staffService;} @ Servicepublic class TbStaffServiceImpl extends ServiceImpl implements ITbStaffService {@ Autowired private ITbDeptService deptService;} 2, error example

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.

Third, problem solving 1. Rough solution

The easiest way is to allow circular references in the global configuration file, which defaults to false, displays declared as true, and avoids console circular reference exceptions when the project starts.

Spring: main: allow-circular-references: true2, elegant solution

Spring officially forbids the use of circular dependencies by default, although there are optional configurations that allow developers to continue to use circular dependencies.

Spring's official intention is that developers do not want to write circular dependent code, that is, a future version may force the use of circular dependencies, so gradually eliminating circular dependencies in new projects is a problem we have to face.

Use the return value of the method to get the instance object, and instead inject the instance object through member variables.

@ Servicepublic class TbDeptServiceImpl extends ServiceImpl implements ITbDeptService {/ * use the method to return the instance object and replace the member variable injection * @ return ITbStaffService * / public ITbStaffService getStaffService () {return SpringUtils.getBean (ITbStaffService.class) } @ Servicepublic class TbStaffServiceImpl extends ServiceImpl implements ITbStaffService {/ * use the method to return the instance object and replace the member variable injection * @ return ITbStaffService * / public ITbDeptService getDeptService () {return SpringUtils.getBean (ITbDeptService.class);}}

The following dependency needs to be used, which is a public dependency extracted by the author and can be used across projects.

Xin.altitude.cms.common ucode-cms-common 1.3.4

If this dependency is not found, it is likely that Aliyun's Maven repository has not been synchronized, so you can force the use of Maven central repository in the project.

Public maven nexus https://repo1.maven.org/maven2/ always IV. Summary

As a widely used framework, Spring ecology has become the mainstream standard for Java enterprise applications, and its small changes have an inestimable impact on the integrated ecology. Transform from follower to guide, resolutely prohibit the problem of circular dependence, reflecting the responsibility as a guide.

Circular reference is used to use, initially looks like there is nothing wrong with the code, think about it is unreasonable design. The direct manifestation of circular dependence is that you have me in you and you in me, which is puzzling from the design of the object.

Most developers always pay attention to the changes in the underlying framework, which will benefit from the application layer. The underlying framework here refers to JDK, Spring ecology, Apache, well-known open source and widely used frameworks, such as guava and so on.

Thank you for reading this article carefully. I hope the article "how to solve the problem of SpringBoot2.6.x disabling circular dependence by default" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report