On spring bean life cycle verification

1、 Looking at bean life cycle from source code comments

From the JDK source code, the beanfactory implementation class needs to support the complete life cycle of the bean. The complete initialization method and its standard sequence (Format: interface method) are as follows:

1. Beannameaware setbeanname setting bean name 2 Beanclassloaderaware setbeanclassloader set bean class loader 3 Beanfactoryaware setbeanfactory set bean factory 4 Environmentaware setenvironment: profiles + properties 5 Embeddedvalueresolver aware setembeddedvalueresolver 6 Resourceloaderaware setresourceloader sets the resource loader, which is only applicable to running 7.0 in the application context Applicationeventpublisheraware setapplicationeventpublisher injects applicationeventpublisher 8 Messagesourceaware setmessagesource setting internationalization support 9 Applicationcontextaware setapplicationcontext set application context 10 Servletcontextaware setservletcontext setting servlet context 11 Beanpostprocessors postprocessbeforeinitialization executes the bean processor pre method 12 Initializingbean afterpropertieset implements the post method 13 after initializing bean sets properties A custom init method finalization 14 Beanpostprocessors postprocessafterinitialization executes the bean processor post method

Destruction sequence:

1. Destructionawarebeanpostprocessors postprocessbeforedestruction2 Disposablebean destroy bean destroy callback method 3 A custom destroy method finalization

Core method flow chart:

2、 Test verification

2.1 analysis principle

All the 17 steps mentioned above can be tested. We select 8 of them to test as follows:

Initialization process:

1. Beannameaware setbeanname setting bean name 3 Beanfactoryaware setbeanfactory set bean factory 11 Beanpostprocessors postprocessbeforeinitialization executes the bean processor pre method 12 Initializingbean afterpropertieset implements the post method 13 after initializing bean sets properties A custom init method finalization 14 Beanpostprocessors postprocessafterinitialization executes the bean processor post method

Destruction process:

2. Disposablebean destroy bean destroy callback method 3 A custom destroy method finalization

2.2 code list

1. Build a person class to implement beanfactoryaware, beannameaware, initializingbean and disposablebean interfaces.

2. Customize the factory post processor, copy the postprocessbeanfactory method, and add the attribute mobile = 110 after obtaining the bean definition

3. Customize the bean post processor and duplicate the two methods postprocessbeforeinitialization and postprocessafterinitializationto correspond to the operations before and after initialization respectively.

4. custom instantiation of bean postprocessing adapter, copying postProcessBeforeInstantiation, postProcessBeforeInstantiation, postProcessPropertyValues corresponds to the instantiation of Bean before calling, instantiating Bean after calling, setting up an attribute when called

Note: the instantiation awarebeanpostprocessoradapter here is instantiation, not initialization!!!

5. Test the class and use classpathxmlapplicationcontext to read the bean configuration file from XML to generate the ApplicationContext container application context. This step corresponds to container initialization. The second part gets the bean from the application context. The last step is to register a closing hook, which is triggered when the container is closed.

Direct operation, the results are as follows:

Summary process:

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>