“This is the 30th day of my participation in the August Text Challenge.

How do we play with spring containerization to implement circular dependencies?

Here we use an XML file to implement application.xml. Two objects, A and B, depend on each other

We suggest that the set injection is ok, then we add the configuration file of The Spring container on the basis, pay attention to the location of the addition;

Start doing spring container instantiation to create two objects for loop dependency;

Matters needing attention:

When you first start, you may encounter that the bean does not exist. Note the location of the configuration file when you create it. Check the location of A and B.

How to solve gracefully:

https://winterchen.blog.csdn.net/article/details/78425230?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommen dFromBaidu-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromBaidu-1.controlCopy the code

The successes are as follows:

When spring container objects, scope= photoType, (prototype) creates A new A or B object each time it is called. We changed the scope property of the configuration file;

Report errors, and constructor injection way, before we are BeanCurrentlyInCreationException: bean object in create an inner loop, an exception occurs, also said that when the scope attribute is not the time for singleton, unable to solve the problem of spring cycles;

 

In fact, we only know that spring circular dependencies are implemented by singleton Spring container or set injection, but I still want to know how to realize the source code at the bottom; Spring loop dependencies are implemented with level 3 caching;

Note the conclusion: Spring internally implements loop dependencies based on level 3 caching;

Three levels of cache

When the interviewer said, spring loop dependency, you can mention level 3 cache, that proves you absolutely have internal work, this belongs to the source code category, can also understand your learning potential, ha ha, open dry: level 3 cache is mainly this class; DefaultSingletonBeanRegistry;

IDEA students can query the specific implementation process of this class by the shift key twice;

This class actually uses three maps to implement a level 3 cache;

What is tertiary caching?

Level 3 caching only applies when the object is a singleton bean object and the bean object is created each time;

Matters needing attention:

Instantiation and initialization are two concepts in Java:

Object properties can only be initialized after the object is instantiated

Instantiation: equivalent to a request for an area, but only in the process of successful application, or unknown data in the initialization: we often say initialization – represents the process of loading properties, various data, start running