Analysis of Spring Bean Life Cycle Source Code

Analysis of Spring Bean Life Cycle Source Code

Overview

Spring has been popular for a long time. It is a lightweight framework written in Java. It is welcomed by many companies and programmers. The Bean life cycle is the whole process of an object from instantiation to destruction. It is very important to understand the life cycle. Necessary.

importance

The life cycle of spring is more complicated, and only by understanding its process and principles can the program be better extended.

Source code analysis

Life cycle process

bean instantiation

Analysis of Spring Bean Life Cycle Source Code

Attribute filling and Aware interface detection setting dependency

Analysis of Spring Bean Life Cycle Source CodeAnalysis of Spring Bean Life Cycle Source Code

If the container customizes the implementation class of BeanpostProcessor, perform the corresponding pre-processing of the processor

Analysis of Spring Bean Life Cycle Source Code

The bean implements the initialization interface InitlializingBean, and the corresponding logic is executed

Analysis of Spring Bean Life Cycle Source Code

At the same time, the initialization method init-method is also customized

Analysis of Spring Bean Life Cycle Source Code

If the container customizes the implementation class of BeanpostProcessor, it will execute the post-processing of the corresponding processor

Analysis of Spring Bean Life Cycle Source Code

Register the destroyed callback bean

Analysis of Spring Bean Life Cycle Source Code

The bean implements the destruction interface DisposableBean, and the corresponding logic is executed

Analysis of Spring Bean Life Cycle Source Code

At the same time, the destruction-method is also customized

Analysis of Spring Bean Life Cycle Source Code

Process request summary

Analysis of Spring Bean Life Cycle Source Code
The above flowchart summarizes the entire process from container startup to destruction

to sum up

1. From the above source code analysis, the life cycle of spring can be divided into four major parts:
1. Instantiation of
bean 2. Setting of bean properties
3. Initialization
of bean 4. Destruction of bean
2. Implementation of Aware interface, then Corresponding dependencies will be injected.
Third, the interface BeanPostProcessor has two initialization methods, these two methods are very important when adding beans, for example, spring aop can return a proxy object; parameter decryption, etc.
Fourth, the life cycle is used Simple factories, factory methods, singleton patterns, adapter patterns, and strategy patterns can be used to design better applications.

Guess you like

Origin blog.51cto.com/xxdeelon/2540442