In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand Spring source 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 "how to understand Spring source circular dependence" bar!
How does Spring solve circular dependency
1. What is circular dependency?
This word, when A Fan heard, must be the same as everyone's reaction, cycle, dependence, that is not A quoted B, while B quoted C, and C quoted A, so a love triangle relationship appeared.
So how do you express it in code?
Public class ClassTestA {private ClassTestB classTestB; public void a () {classTestB.b ();} public ClassTestB getClassTestB () {return classTestB;} private void setClassTestB (ClassTestB classTestB) {this.classTestB = classTestB;}} public class ClassTestB {private ClassTestC classTestC; public void b () {classTestC.c () } public ClassTestC getClassTestC () {return classTestC;} private void setClassTestC (ClassTestC classTestC) {this.classTestC = classTestC;}} public class ClassTestC {private ClassTestA classTestA; public void c () {classTestA.a ();} public ClassTestA getClassTestA () {return classTestA;} private void setClassTestA (ClassTestA classTestA) {this.classTestA = classTestA }}
two。 What's wrong with circular dependency?
In Ah Fan's impression, the most direct problem with circular dependency is that it will occur in the instantiation of objects. When creating objects, if you add this A dependency B to the configuration of Spring, and B depends on Cpene C to rely on A, then there will be an error when creating an instance object of A.
And if the dependence of this kind of circular invocation does not end him, then it is equivalent to an endless cycle, just like A Fan a few days ago when maintaining the project before "16 years", all kinds of memory overflowed, indicating that he was very depressed.
Spring also divides the handling of circular dependencies into different situations. Ah Fan will take you to have a look.
3.Spring cyclic dependency processing-(constructor cyclic dependency)
Constructor circular dependency means that through construction and injection of circular dependency, and this kind of dependency, there is no way to solve, if you dare to force dependence, do not mean, there is an exception you have not seen for a long time when BeanCurrentlyInCreationException appears this exception, it means the problem of circular dependency.
I believe that when there is an exception, when you do not understand why, the first time, copy the abnormal information, put it on Baidu, or Google to query, BeanCurrentlyInCreationException on Baidu, clear at a glance.
In the Spring configuration file, if you configure the circular dependencies of A, B, C in this way, when you create A, you find that the constructor needs class B, and then you create B, and when you create B, you find that you need C, and then you create C, and when you create C, you find that you need A, so you turn around and go back, so you form a closed loop, and there is no way to create it.
In this case, Spring instantiates bean through the ApplicationContext.getBean () method. If the object you want to get depends on another object, it first creates the current object, then gets the dependent object by recursively calling the ApplicationContext.getBean () method, and finally injects the acquired object into the current object. As A Fan said just now, a closed loop has been created, so there is no way to create it.
4.Spring cyclic dependency processing II (setter cyclic dependency)
Setter cyclic injection refers to the circular dependency formed by setter injection. In this way, Spring can solve the problem.
The dependency caused by using setter injection is accomplished through the Spring container to expose the bean of the newly completed construction injector in advance, but the other steps have not been completed at this time.
At this point, we need to expose a singleton factory method in advance and let other bean refer to this bean.
AddSingletonFactory (beanName,new ObjectFactory () {public Object getObject () throws BeanException {return getEarlyBeanReference (beanName,mbd,bean)}})
When Spring creates A, it creates A based on no-parameter construction, and exposes that ObjectFactory is used to return a pre-exposed bean and then setter to inject it.
Similarly, both B and C look like this, and the setter injection can be completed at this time.
5.Spring circular dependency processing 3 (scope circular dependency)
Ah Fan, take a look at this configuration.
For "singleton" scope, he can base circular dependencies through "setAllowCircularReference (false)".
It is also flawed, and this approach can only solve singleton-scoped bean circular dependencies.
And if Spring solves circular dependencies, everyone will be curious, saying that it is a three-tier cache, so please find the source code of your Spring.
Org.springframework.beans.factory.support.AbstractBeanFactory#doGetBean thank you for your reading, the above is "how to understand Spring source circular dependence" content, after the study of this article, I believe you on how to understand Spring source circular dependence of this problem have a deeper understanding, the specific use of the need for you to practice verification. 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.