In-depth Spring Bottom Dialysis Bean Creation Process: Seeing the Day after the Clouds

foreword

First of all, thank you for your reading. There are personal thoughts and conclusions in the article. It is inevitable that there will be omissions. If there are mistakes, please correct and supplement. Finally, I hope you can also gain something.


1. BeanFactory quick start

1. BeanFactory creates Bean

insert image description here
IOC control inversion, the inversion is the right to create beans

Combined with the above figure, there are the following steps:

1) Import the Spring jar package or Maven coordinates;
2) Define the UserService interface and its UserServiceImpl implementation class;
3)
Create the beans. Load the configuration file and get the UserService instance object.


The following are the steps to create beans with BeanFactory

insert image description here

Are you familiar with the above creation process? Yes, it’s just one more step than using ApplicationContext, and creating more factory objects.It can be seen that the process of creating beans in the above figure is quite troublesome, so the ApplicationContext behind encapsulates it, and finally simplified as follows

insert image description here

You can look at the relationship between the two (shortcut key CTRL + alt + u)

insert image description here


2. The relationship between BeanFactory and ApplicationContext

insert image description here

It is the parent interface of ApplicationContext
and is the core container of Spring, and the main ApplicationContext implementations combine its functions (BeanFactory is actually a member variable of ApplicationContext


3. Differences from ApplicationContext (high-frequency questions)

ApplicationContext (application context) Spring's container, in fact, its underlying operation of calling beans is still BeanFactory. ApplicationContext internally encapsulates BeanFactory, which is richer and more powerful than BeanFactory

The difference between BeanFactory and ApplicationContext

1) BeanFactory is an early interface of Spring, called Spring's Bean factory, and ApplicationContext is a later more advanced interface, called Spring container;

2) ApplicationContext extends the functions on the basis of BeanFactory, such as: monitoring function, internationalization function, etc. The API of BeanFactory is more low-level, and most of the APIs of ApplicationContext are the encapsulation of these low-level APIs;

3) The main logic and functions of Bean creation are encapsulated in BeanFactory. ApplicationContext not only inherits BeanFactory, but also maintains the reference of BeanFactory inside ApplicationContext. Therefore, ApplicationContext and BeanFactory have both inheritance and integration relationships.

4) Bean initialization timing is different,One lazy loading, one immediate loading. The original BeanFactory creates beans when getBean is called for the first time, while ApplicationContext loads configuration files, and all beans are instantiated and initialized as soon as the container is created.

insert image description here


4. BeanFactory inheritance system

BeanFactory is the core interface. There must be a specific implementation involved in the project running process. This specific implementation is DefaultListableBeanFactory , and the implementation class of Beanfactory maintained inside ApplicationContext is also it


insert image description here


5. ApplicationContext inheritance system

Only in the Spring basic environment, that is, when only spring-context coordinates are imported, the inheritance system of ApplicationContext at this time

insert image description here

The functions of the three ApplicationContexts commonly used in the spring environment are as follows:

insert image description here


2. The basic process of Bean instantiation (emphasis)

bean的实例化总共以下5步,看明白了可以说是拨云见日,前后知识点都能衔接

①When the Spring container is initialized, it will configure the xml configuration information (Note that what is encapsulated at this time is not the object, but the information of the object configuration in xml, such as attributes) encapsulated into a BeanDefinition object (bean definition object)
encapsulates the bean tag, which is the following
insert image description here


② Store all BeanDefinitions in a Map collection named beanDefinitionMap, and then the Spring framework traverses the Map, takes out each BeanDefinition object, and obtains the information encapsulated inside the BeanDefinition.Mainly take the class name of the bean(Because the bean object will be created based on reflection later, if you forget about reflection, you can read this article: Introduction to the use of Java reflection )
insert image description here

Do you want to know what the Map collection of beanDefinitionMap is? I saw its location when I flipped through the source code, which is a map collection defined in DefaultListableBeanFactory, which is dedicated to storing bean label objects, and DefaultListableBeanFactory is the implementation class of BeanFactory, which is It is maintained in BeanFactory, so you should understand it.


③Use reflection to create Bean instance objects


④The created Bean object is stored in a Map collection named singletonObjects (singleton pool, also maintained in BeanFactory)


⑤ When the getBean method is called, the Bean instance object is finally taken out from the Map collection and returned


Final breakpoint debugging verification instructions

insert image description here

insert image description here

So, both beanDefinitionMap and singletonObjects are maintained in beanFactory

Finally, the basic process of bean creation can be summarized as the figure below, and the subsequent expansion functions are also continuously expanded in the figure below

insert image description here


Guess you like

Origin blog.csdn.net/giveupgivedown/article/details/129122934