Spring Level 3 Cache and Why Not Use Level 2 Cache
How does spring use Level 3 cache to solve circular dependencies?
First of all, the beans we discussed below are not multi-case, and multiple-case beans do not exist in the cache, so spring cannot solve the circular dependence of multiple beans.
We know that the initialization process of beans is divided into three steps: instantiation, dependency injection, and initialization.
First, let’s take a look at what is stored in the Level 3 cache:
- Level 1 cache: stores beans that have been completely initialized, which is also the beans we get in the program.
- Level 2 cache: The bean that stores instantiation completed, but does not perform dependency injection and initialization
- Level 3 cache: The bean has not been instantiated yet, and the bean creation factory is simply saved.
This caching mechanism can help us solve such a circular dependency:A is instantiated before B, and A needs to setter inject B, and B needs to inject A (or attribute inject A).
Notice:
- When B obtains a reference to A, the properties of A can be set later.
- However, the constructor of B must be generated and does not generate any references. That is to say, B must obtain A, even if A has not set the attributes yet.
- If B is instantiated before A, then B will not be able to quote A and will directly report an error.
In this way, the following process will be carried out during the initialization phase:
1: A needs to initialize, instantiate first, put A's creation factory in the third level cache, but A finds that B has not appeared in its own attribute injection, so he will first initialize B
2: B needs to initialize, instantiate first, put B's creation factory in the third-level cache type, and then B finds that its attribute injection requires A. Go to level one and level two cannot find A. He found A's creation factory at level three, so he calls the getobject() method of the creation factory to create an instantiated object of A (if A implements aop, it is the agent object of A created), put it in the second-level cache and add it to its own constructor/attribute (A property can be allowed to be set later, but it cannot be left out). In this way, B can complete its own initialization, put it in the first-level cache and delete the second-level B cache after the initialization is completed.
3: The initialization of B is completed. A continues to assign attributes and successfully obtains the instantiated and initialized object B from the first-level cache. The creation of object A is also completed. Delete A in the first-level cache, and put A into the first-level cache at the same time.
Why do you need Level 3 cache instead of Level 2?
1 Avoid duplication. If there are only two-level caches of 1 and 3, then when A needs BC and BC both need A, BC will call A's creation factory at the same time, generating two copies of A, resulting in repeated creation.
2 Spring hopes that the life cycle of the proxy and the bean must be separated. If the creation factory is called directly from the beginning, then all beans with AOP will be directly dynamically proxyed, but Spring hopes that the final proxy will be generated after the bean is initialized. If it is not separated, it means that level 3 cache is not required, level 1 stores beans, level 2 stores instantiated beans, and all beans are directly called to create factory and placed in level 2 cache, so there is no problem.
Using level three cache, if B needs A injection in this process and A has a proxy, an early proxy object will be generated to B for solving circular dependencies. The final proxy object still needs to continue after beanA has completed the attribute filling and initialization. There is a difference between these two generated proxy objects, so it does not really destroy the principle of Spring agent after the initialization of the Bean, because Spring still ensures that the final proxy object is generated after the Bean is fully initialized.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.