Complete the spring bean lifecycle.

1) Instantiate the Bean object (allocate memory space on the JVM heap).

2) Initialize the Bean object (the set() method injects property values).

3) If the Bean object implements the BeanNameAware interface, you need to implement the setBeanName() method to set the Bean name.

4) If the Bean object implements the BeanClassLoaderAware interface, call the setBeanClassLoader() method, passing in an instance of the Classloader object.

5) if you have, and load the beans are related to the spring container BeanPostProcessor object to perform postProcessBeforeInitiallization () method

6) If the Bean implements the InitializingBean interface, execute afterPropertiesSet(); If the Bean definition in the configuration file contains the init-method attribute, the specified method is executed.

7) if you have, and load the Bean Spring container of related objects BeanPostProcessor, perform postProcessAfterInitialization () method.

8) When you want to destroy the Bean, if the Bean has DisposableBean interface, execute **destroy() **; If the Bean definition in the configuration file contains the destroy-method attribute when it is destroyed

What are Spring’s cyclic dependencies?

@Component
publicclass A {

    @Autowired
    private B b;

}
Copy the code
@Conponent
publicclass B {

    @Autowired
    private A a;
}

Copy the code

How to solve spring’s loop dependency?

/** Cache of singleton objects: */ / private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256); */ Private final Map<String, ObjectFactory<? >> singletonFactories = new HashMap<>(16); /** Cache of early singleton objects: bean name to bean instance. */ private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16); /** The bean will be placed in the cache during creation, and the cache will be removed after successful creation. private final Set<String> singletonsCurrentlyInCreation = Collections.newSetFromMap(new ConcurrentHashMap<>(16));Copy the code

Can level 1 cache solve cyclic dependencies?

A: No. In the three levels of cache, objects are placed differently (1, instantiated and initialized, 2, instantiated and uninitialized), but with only one level of cache, it is possible to fetch uninitialized objects with null property values under concurrent conditions.

Can level 2 cache solve cyclic dependencies? Why use level 3 caching to solve loop dependencies?

A: Two-level caching is theoretically possible. But a word of caution: Why prevent anonymous inner classes in level 3 caching?

The essence is that in order to create proxy objects, if you have class A now, you need to generate proxy objects, you need to instantiate them.

The level 3 cache places an anonymous inner class that generates a concrete object. This anonymous inner class may bea proxy class or a plain instance object. Using level 3 cache ensures that whether or not you want to proxy an object, you are using an object instead of a plain bean followed by a proxy class. Ensure consistency.

Why can’t the constructor approach solve the problem of loop dependency?

Answer: because in this way to instantiate and initialize the separated (instantiation to attribute assignment), will be instantiated in advance good object exposed out ahead of schedule, call for others, while the use of the constructor, must call a constructor, there is no constructor cannot complete object instantiated operation, thus in an infinite loop.