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 circular dependency of spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "spring how to solve circular dependence", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "spring how to solve circular dependency" bar!

First of all, spring solves the problem of circular dependencies through a three-level cache mechanism, with the exception of constructor dependencies, that is, constructor injection may report errors.

Circular dependencies are as follows:

First, the steps for spring to initialize bean are as follows:

It only needs to be understood simply as three steps:

Instantiation

Populate attributes (injection dependencies)

Initialization complete

Spring will first go to the first-level cache (singletonObjects) when creating Bean A, if it does not have the first-level cache, then get it from the second-level cache (earlySingletonObjects). If it does not have the second-level cache, it will get it from the third-level cache (singletonFactories). If it cannot get it yet, instantiate an A, then put it into the third-level cache, and then populate the attribute. Now it finds that it depends on B, so it creates B, and goes through the same steps above. Since each level of cache can not be obtained, instantiate B, then fill in the attribute, find the dependency A, and then go to each level cache in turn to get it. Because there is already an An in the three-level cache, B can successfully inject the dependency and be initialized correctly, and then return recursively, so A can also be initialized correctly.

From the above explanation, you can see that bean needs to be instantiated first, so that's why constructor dependencies may fail. If A constructor depends on B, because instantiation A needs to call A's constructor first and finds that it depends on B, then B needs to initialize B, but B also depends on A, no matter whether B is injected through constructor or setter, at this time, because An is not instantiated and is not put into the third-level cache, B cannot be initialized, so spring will report an error directly. Conversely, if An is injected through setter, you can instantiate it, put it in the cache, and then populate the property through the constructor, so that B can be obtained in the cache regardless of whether B is injected into A through setter or constructor, so it can be initialized.

Thus, in circular dependencies, if the bean created by spring first injects another bean through the constructor, it will fail, and vice versa. So that's why it must be difficult to inject through the constructor.

Thank you for reading, the above is the content of "how to solve the circular dependence of spring". After the study of this article, I believe you have a deeper understanding of how to solve the problem of circular dependence of spring, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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

Internet Technology

Wechat

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

12
Report