[Spring uses the third-level cache to solve the process of circular dependencies]

  1. The first level cache stores instantiated objects.
  2. The second-level cache stores objects that have been created in the memory space but have not yet been assigned properties.
  3. The third-level cache stores object factories, which are used to create objects that are exposed to beans in advance.
@Service
public class TestService1 {
 
    @Autowired
    private TestService2 testService2;
 
    public void test1() {
    }
}
@Service
public class TestService2 {
 
    @Autowired
    private TestService1 testService1;
 
    public void test2() {
    }
}

insert image description here

testService1 first goes to the first-level cache to see if there is an instance. If it is found, it continues to go to the second-level cache to check. If it is found, go to the third-level cache to view. If there is no instance, create an instance. in the cache.

At this time, attribute assignment is performed, and it is found that there is still a testService2, which has no assignment and is empty. Just check whether there is an instance of testSerivce2 in the first-level cache. If you find no, go to the second-level to check if there is no, and go to the third-level cache to check. If it is found not, it will create an instance, expose it in advance, and add it to the L3 cache.

At this time, it is found in the testSerivce2 object that there is no assignment in testService1, and then assigns value to testService1, checks from the first-level cache, and finds no, goes to the second-level to check, and finds no, goes to the third-level to view, and finds that there is, put the instance testService1 from the third-level The cache is added to the second-level cache, and the instance of the third-level cache of the instance testService1 is deleted. At this time, there are instance objects in testService2, and testService1 in the object also has a value, which is an instance object that can be used. Move this object to In the first-level cache, delete the testService2 in the third-level cache.

At this time, the testService2 attribute in testService1 can obtain the testService2 instance from the first-level cache, assign it to fill, testService1 has also completed the instantiation, move testService1 from the second-level cache to the first-level cache, and put testService1 in the second-level cache. Instances of the level cache are also removed.

insert image description here

Guess you like

Origin blog.csdn.net/java_wxid/article/details/123201167