In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to solve circular dependency in Spring. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
First, what is circular dependency?
Circular dependency, in fact, is circular reference, that is, two or more bean refer to each other, and finally form a closed loop, such as A depends on B, B depends on C, C depends on A. As shown in the following figure:
Circular dependency in Spring is actually a process of an endless loop. When initializing A, you find that you depend on B, then you initialize B, and then you find that B depends on C, runs to initialize C, and initializes C when you initialize C. when you initialize C, you initialize A, and the loop never exits in turn, unless there is a termination condition.
In general, there are two kinds of Spring circular dependencies:
The circular dependency of the constructor. The circular dependency of the field property. Spring cannot solve the circular dependency of the constructor, and can only throw a BeanCurrentlyInCreationException exception to indicate the circular dependency, so what we analyze below is the circular dependency based on the field attribute.
In the previous article, the loading of Bean for Spring Ioc source code analysis (3): it is mentioned in the Bean creation of each scope that Spring only solves the circular dependency that scope is singleton. For the bean whose scope is prototype, Spring cannot solve it and directly throws a BeanCurrentlyInCreationException exception.
Why doesn't Spring deal with prototype bean? In fact, if you understand how Spring solves the circular dependency of singleton bean, you will understand. Leaving a question here, let's take a look at how Spring solves the circular dependency of singleton bean.
Second, solve the circular dependence of singleton
In the doGetBean () method of AbstractBeanFactory, when we get the Singleton Bean based on BeanName, we first get it from the cache.
The code is as follows:
/ / DefaultSingletonBeanRegistry.java@Nullableprotected Object getSingleton (String beanName, boolean allowEarlyReference) {/ / load bean Object singletonObject = this.singletonObjects.get (beanName) from first-level cache singletonObjects / / the bean in the cache is empty and the current bean is creating an if (singletonObject = = null & & isSingletonCurrentlyInCreation (beanName)) {/ / locked synchronized (this.singletonObjects) {/ / get singletonObject = this.earlySingletonObjects.get (beanName) from the secondary cache earlySingletonObjects It is not in / / earlySingletonObjects, and it is allowed to create if in advance (singletonObject = = null & & allowEarlyReference) {/ / get the corresponding ObjectFactory ObjectFactory singletonFactory = this.singletonFactories.get (beanName) from the third-level cache singletonFactories If (singletonFactory! = null) {/ / get bean singletonObject = singletonFactory.getObject () from the singleton factory; / / add to the secondary cache this.earlySingletonObjects.put (beanName, singletonObject) / / remove this.singletonFactories.remove (beanName) from the tertiary cache;}} return singletonObject;}
The three key variables involved in this code are three levels of caching, defined as follows:
/ * * Cache of singleton objects: bean name-- > bean instance * / / the cache level 1 cache of singleton bean private final Map singletonObjects = new ConcurrentHashMap; / * * Cache of singleton factories: bean name-- > ObjectFactory * / / singleton object factory cache level 3 cache private final Map
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.