Spring's third-level cache solves circular dependencies

1. What is Spring third-level cache?

First-level cache: Also called a singleton pool, it stores Bean objects that have gone through a complete life cycle.

Second-level cache: Stores Bean objects exposed early. After instantiation, the objects are placed in this Map. (The Bean may have only been instantiated, and the properties have not yet been populated).

The third level cache: the factory that stores early exposed beans.

Note:

Only singleton beans will be exposed in advance through the third-level cache to solve the problem of circular dependencies. Non-singleton beans will be re-created every time they are obtained from the container. Therefore, non-singleton beans are If there is no cache, it will not be placed in the third-level cache.

In order to solve the problem of AOP generating new objects in the second-level cache, Spring performs AOP in advance. For example, in the process of loading b, if a circular dependency is sent and b depends on a, it is necessary to perform AOP on a and obtain the enhanced information in advance. a object, so that the a object that b object depends on is the enhanced a.

The second and third level caches are to solve circular dependencies, and the reason why they are second and third level caches instead of second level caches is mainly to solve the problem that circularly dependent objects need to be proxied by aop in advance, and if there are no circular dependencies, early beans will not be truly Exposed, there is no need to execute the agent process in advance, and there is no need to execute the agent process repeatedly.

 2. The creation process of objects in the third-level cache
A depends on B, and B depends on A

1. B is needed during the creation process of A, so A is first placed in the third-level cache to instantiate B.

2. During the instantiation of B, it is found that A is needed, so B first checks the first-level cache to find A. If not, then checks the second-level cache. If not, then checks the third-level cache. When A is found, then the third-level cache The A inside is placed in the second-level cache, and the A in the third-level cache is deleted.

3. B is successfully initialized and placed in the first-level cache (at this time, A in B is still in the state of being created). Then come back and create A. At this time, B has been created. You can get B directly from the first-level cache to complete the creation of A and put A in the first-level cache.

 

Guess you like

Origin blog.csdn.net/jbossjf/article/details/127156436