Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize springboot interdependence and server mutual reference

Shulou Source: shulou.com Published: 2022-06-01 11:56:08 09月18日 Update

Editor to share with you how to achieve springboot interdependence server mutual reference, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Springboot interdependencies server cross-references public class RoleService {@ Autowired @ Lazy private UserServer userServer;} public class UserService {@ Autowired @ Lazy private RoleService roleService;} spring to solve the problem of interdependencies constructor circular dependency @ Servicepublic class A {public A (B b) {}} @ Servicepublic class B {public C (C) {} @ Servicepublic class C {public A (An a) {}}

The circular dependency formed by constructor injection cannot be solved, and only a BeanCurrentlyInCreationException exception can be thrown to indicate the circular dependency.

Description: to create class A, the constructor needs class B, which will create B, and when you create B, you will find that you need class A, and then you will create class C. finally, you will find that you need A when you create C, thus forming a ring.

Principle: the Spring container puts each bean identifier being created in a "currently created bean pool", and the bean identifier will remain in this pool all the time, because if you find that you are already in the "currently created bean pool" in the process of creating bean, it will throw a BeanCurrentlyInCreationException exception to indicate a circular dependency; and the created bean will be cleared from the "currently created bean pool".

Setter circularly depends on @ Servicepublic class A1 {@ Autowired private B1 b1;} @ Servicepublic class B1 {@ Autowired public C1 C1;} @ Servicepublic class C1 {@ Autowired public A1 A1;}

Circular dependencies formed by setter injection.

Principle: the dependency on setter injection is accomplished by exposing the bean that has just completed the constructor injection but not the other steps (such as setter injection) in advance by the Spring container, and can only solve the bean circular dependency of the singleton scope.

Field attribute injection cyclic dependency (prototype) @ Service @ Scope ("prototype") public class A1 {@ Autowired private B1 b1;} @ Service @ Scope ("prototype") public class B1 {@ Autowired public C1;} @ Service @ Scope ("prototype") public class C1 {@ Autowired public A1 A1;}

Error

Make a brief summary

Spring can only solve the circular dependency between singleton bean injected by Setter method.

ClassA depends on both ClassB,ClassB and ClassA, forming a dependent closed loop. When Spring acquires an instance of ClassA, it adds its exposure to the bean cache being created without waiting for the ClassA to be created. When parsing the properties of ClassA, it is found that it depends on ClassB, and get it again.

ClassB, when parsing the attributes of ClassB, it is also found that the attributes of ClassA are needed, but at this time, the ClassA has been exposed in advance and added to the cache of the bean being created, so you do not need to create a new instance of ClassA and get it directly from the cache. So as to solve the problem of circular dependence.

The above is all the content of the article "how to achieve springboot interdependence and server cross-reference". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Loops attributes constructors being articles caches content principles instances containers identifiers procedures problems not much lieutenant generals interactions functions again methods Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Docker NVidia Xiaomi vpn