Analysis of spring circular dependency policy

Cyclic dependence

The so-called circular dependency means that the dependency relationship between multiple beans forms a closed loop, such as a - > b - > C - >... - > A in this case, of course, the simplest circular dependency is the interdependence between two beans: a - > b (a depends on B), B - > A (b depends on a). In spring, if a - > b, B will be created in the process of creating a, and B - > A is found in the process of creating B (or B's dependency). At this time, circular dependency occurs.

Solution of circular dependency

Circular dependency in spring only when

1. Bean is a singleton, 2 Injection through attributes

When these two conditions are met, there is no problem. However, if the dependency is through the constructor or not in singleton mode, the circular dependency will throw an exception, beancurrentyincreationexception. Let's analyze why from the code level.

Circular dependency of prototype

Why do you first introduce the circular dependency of prototype? By the way, you can introduce the core process of creating beans in spring: the dogetbean method in abstractvector. This method is very long. Only the core logic is written here, and personal understanding is indicated on the notes:

It can be seen that the circular dependency of prototype is considered in this process. As long as there is a circular dependency in the bean that creates prototype, an exception will be thrown. However, in the case of singleton, it is solved in another way.

Construction and injection of singleton's cyclic dependency

In the above introduction, a key point appears:

This getsingleton involves the objectfactory interface class. The function of this interface is similar to that of factorybean, but it is mainly used to solve circular dependency. The initialization process is the same as the singleton object returned. About the creation of singleton objects, let's introduce the defaultsingletonbeanregistry class, which is mainly used to help create singleton patterns. The main properties are:

Now look back at the implementation of getsingleton (beanname, new objectfactory < Object > ().

Is this logic similar to solving the loop in prototype? In fact, it calls objectfactory's GetObject () to get the object. Looking back at the previous code, objectfactory's GetObject () method actually calls createbean (beanname, args). When it comes to createbean (beanname, args), I have to say that the main function of abstractautowirecapablebeanfactory is to complete the creation of dependency injected beans. The createbean method code of this class is as follows. Note the notes:

The notes have indicated my understanding. I won't repeat it

summary

I wrote the above code while debugging. Now that I've finished writing it, I'll summarize it according to my understanding.

Related class description

Abstractbeanfactory, this class contains the main process of bean creation, and the dogetbean method contains the processing of prototype circular dependency. The logic is very simple. If a circular dependency occurs, an exception is thrown directly

Defaultsingletonbeanregister is used to manage the creation of singleton objects and solve the problem of circular dependency. The key attribute to solve circular dependency is earlysingletonobjects. It will temporarily cache the objects that have been successfully constructed but whose attributes have not been injected during the construction of singleton objects, so as to solve the problem of circular dependency.

Abstractautowirecapablebeanfactory, the logic related to automatic injection, and the creation, initialization and injection of objects automatically injected by the package. However, if a circular dependency is found in the calling constructor, an exception is thrown

Objectfactory, the interface function is similar to that of factorybean, but in order to solve the circular dependency, it determines whether the getsingleton () obtained is a finished product or a semi-finished product.

reflection

If a -- Construction dependency - > b, B -- attribute dependency -- > A, for example:

Will this be abnormal? Tip: all possible

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>