Review of Spring Core Issues 1: The general process of creating an ioc container, the life cycle of a bean, and why a three-level cache is needed to solve circular dependencies

1. How to obtain the beans in the spring container?

Beans in the spring container can be parsed into objects in the spring container through xml files or annotations:
insert image description here

2. What is the approximate creation process of the spring container?

Spring容器创建的大致过程有如下5个步骤:
1) Parse bean definition information through BeanDefinitionReader
2) Fill in the placeholder information in bean definition information through beanFactoryPostProcessor to obtain a complete beanDefinition object
3) One of the bean life cycle: create a bean object
4) Bean life cycle The second: use the bean object
5) The third of the bean life cycle: destroy the bean object
insert image description here

3. What is beanFactory?

First of all, beanFactory can basically be understood as a spring container, but it is a root interface. We use ApplicationContext就是它的子接口.

beanFacroty defines all the specifications of the bean in this container, such as implemented methods and so on.
insert image description here

insert image description here

4. How to understand postProcessor?

During the container creation process, there will be a lot of postProcessors in spring, and some places are translated as post-processors, which can easily confuse people .

In fact, it would be more appropriate to call it an enhanced processor. For example, in 2, the general creation process diagram of the spring container, when the beanDefinition is loaded, the properties of some bean placeholders are not assigned, and these initial values ​​​​are configured in the configuration file (properties). At this time, you need to replace. Then you can call beanFactoryPostProcessor for processing at this time.

BeanFactoryPostProcessor can be used to enhance objects in beanFactory, and beanPostProcessor can be used to enhance custom beans.
insert image description here

insert image description here

5. What is the bean life cycle?

The bean life cycle can be roughly divided into the following 5 steps, of course the core is the first 3 steps.
1) Bean instantiation
2) Bean attribute assignment
3) Bean initialization
4) Bean use
5) Bean destruction

It is important to note here that AOP is obtained when the bean object executes postProcessorAfterInitialization , and the premise of generating AOP is to add it to the configuration class @EnableAspectJAutoProxy注解. This annotation will eventually register a component with the id internalAutoProxyCreator and type AnnotationAwareAspectJAutoProxyCreator for the spring container. , it is precisely because the component is registered that the wrapIfNecessary method is called. In this way, the general principle of AOP is basically introduced. For a detailed analysis of this piece, see:
Spring annotation-driven development learning summary 12: AOP principle- @EnableAspectJAutoProxy annotation role analysis
insert image description here

6. What is the difference between BeanFactory and FactoryBean?

The same point :
First of all, both BeanFactory and FactoryBean are interfaces, and both are used to create beans. This must first be clear and clear.

Differences :
Through the introduction of Sections 2, 3, and 5, it can be clearly understood that beanFactory is equivalent to our spring container. It is the root interface, defines the basic specifications of the spring container, and defines the complete life cycle of a bean.

Then if we customize a bean that does not want to go through such a complicated life cycle, and at the same time hope that the spring container can add this bean to the container, then we can implement the FactoryBean interface for this bean . Only need to implement its three methods, it can be successfully injected into the spring container. Such a bean is called a factoryBean.

insert image description here

7. Spring solves circular dependencies, why does it need a three-level cache? Can it be solved by using the second level cache?

First of all, clarify the reason for the circular dependency. It is that two or more objects are interdependent. When the bean is assigning attributes, it will find the bean it needs. When it is interdependent, there is a cycle. Dependency problem.

The core of solving the circular dependency problem is actually one point:
the separation of instantiation and initialization process. Before the attribute assignment, it has been instantiated;

To sum up, in fact, using two caches can solve the circular dependency problem .

So why does Spring need a third-level cache? You can first look at Spring’s three-level cache object:
1) Level-1 cache: singletonObjects: Map<String, Object>
2) Level-2 cache: earlySingletonObjects: Map<String, Object>
3) Level-3 cache: singletonFactories: Map<String , ObjectFactory<?>>

You can see that what is saved in the third-level cache singletonFactories is ObjectFactory, why is the object saved in the third-level cache ObjectFactory? The first thing to understand ObjectFactoryis what it is.

ObjectFactoryis a functional interface. You can put a lambda expression as a parameter in the actual parameter of the method. When the method is executed, the current lambda expression will not be actually called. Only when the getObject method is called will the lambda expression be called.

In the fifth summary, in postProcessorAfterInitialization, if the bean is AOP-enhanced, then one will be generated at this time 代理对象, and it will create a new proxy object through new. There is a problem in this way, that is, it is not known when the container will obtain the bean. Therefore, in order to generate a proxy object when the container really needs the bean, the spring container will put the proxy object into the second-level cache. Therefore, the object saved in the third-level cache is ObjectFactory.

In another way of explaining, see if it can help you understand: when an object is exposed or referenced by other objects cannot be determined in advance, so only at the moment it is called can it be judged that it is the original object Still a proxy object. The use of lambda expressions is similar to a callback mechanism. When not exposed, there is no need to call and execute. When it needs to be called, the lambda expression is actually executed to determine whether the returned object is the original object or the proxy object.

Therefore, the third-level cache is mainly designed for proxy objects of AOP functions.
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/xueping_wu/article/details/126437729