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

The method of solving circular dependency by spring

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

Share

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

This article introduces the relevant knowledge of "spring's solution to circular dependence". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, take a look at spring's simple process for creating a bean. If beanA references beanB,beanB to beanA,spring, it will cause circular dependencies when initializing beanA (here we are talking about singletons, and the underlying spring only solves singleton circular dependencies).

Before recording, I wrote two test classes to restore circular dependencies, one is ClassA, which refers to ClassB, and ClassB also refers to ClassA. In this way ClassA and ClassB form circular dependencies.

When creating the ClassB Bean, the bottom layer of the spring container will call getSingleton to get it from the first-level cache singletonObjects. If there is no first-level cache, go to the second-level cache earlySingletonObjects, and if it is not in the second-level cache, go to the third-level cache singletonFactories to get it. If there is none, call the createBean method to start creating the ClassB, the Bean.

Createbean then calls the doCreateBean method, which actually executes the method that creates the bean. This method calls createBeanInstance (beanName, mbd, args) through the post processor to call the constructor of ClassB and create an instance object that returns ClassB. In this case, the reference to the ClassA of the object must be empty, because the default constructor of ClassB does not assign a value to ClassA. Spring then puts this early object into the third-level cache singletonFactories.

When you enter the resolveReference method, spring will first determine whether the current container has a parent container, and if so, get the reference object ClassA from the parent container, and get the reference object ClassA from the current container if not. (spring allows the child container to use the bean of the parent container, as shown here, such as springmvc.) at this point, the program will call this.beanFactory.getBean (resolvedName). Get the reference object ClassA again from the container. At this point, getBean will call doGetBean to re-create the ClassB object process.

Similarly, when spring creates the reference object ClassA of ClassB, it also parses the reference object of ClassA. At this point, the reference object of the ClassA is ClassB (when the ClassB object is exposed in the three-tier cache), it is time to call the current container's getBean--- > doGetBean and call the getSingleton (beanName) method in doGetBean. Here we will see that spring is conditional on fetching an object from a secondary or tertiary cache, even if the object is being created. Through the method of isSingletonCurrentlyInCreation (beanName) to judge.

At this point, the reference to the ClassA object ClassB has already got the value, so the creation goes all the way down to the addSingleton (beanName, singletonObject) method, where the ClassA is put into the first-level cache singletonObjects and removed from the second-level cache and the third-level cache. Because the bean for ClassA has been created, the secondary and tertiary caches are useless. At this point, the bean of the complete ClassA is returned, the property ClassA of ClassB is assigned, and the bean of ClassB can continue to be created.

Note: spring cannot solve the circular dependency by assigning values to attributes through the constructor. From the above analysis, we can see that spring executes the createBeanInstance method to determine the constructor that calls the current bean, which has not yet been put into the three-level cache. If the bean scope is a prototype, it will not solve the circular dependency, because the prototype object is not placed in the cache.

This is the end of the content of "spring's solution to circular dependency". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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