The scope and life cycle of Spring beans

Source: Internet
Author: User

Scope of the Bean

1.singleton,prototype, Web environment: Request,session,gloab session

2. Configure by Scope= ""

3. For the singleton spring container, only one shared instance is created, and prototype creates a different instance

Bean lifecycle (Bean's own method, bean-level life-cycle interface method, container-level life-cycle interface method)

It is divided into beanfactory life cycle and ApplicationContext life cycle.

1, for Beanfactory

1, when invoking a bean through Getbean, first look at whether the container is registered (not the bean implements the interface, belongs to the container-level life cycle method) the Instantiationawarebeanpostprocessor interface, If it is registered, the Postprocessbeforeinstantiation () method will be called.

2. Then instantiate the bean

3. Call the Postprocessafterinstantiation () method of the Instantiationawarebeanpostprocessor interface

4. Set properties, call Setter ()

5. Call Beannameaware's Setbeanname () method

6. Call the Setbeanfactory () method in Beanfactoryaware

7. Call the Postprocessbeforeinitialization () method of the Instantiationawarebeanpostprocessor interface

8. If the container registers the Beanpostprocessor interface, call the Postprocessbeforeinitialization () method of the interface

9. Call the Afterpropertiesset () method of the Initializingbean interface

10. Call the Init-method property configuration method

11. Call the Postprocessafterinitialization () method of the Instantiationawarebeanpostprocessor interface

12. Call the Postprocessafterinitialization () method of the Beanpostprocessor interface

13, if the Bean is a singleton, the bean instance is stored in the buffer pool by the spring container to continue the relationship Bean's life cycle, otherwise the call to the Bean is managed

14. Method of calling Disposablebean interface

15, through the Destory-method property configuration method

To manually add the Beanpostprocessor implementation Class (Configurablebeanfactory) bf.addbeanpostprocessor (New Mybeanpostprocessor ()) to the IOC container;

The life cycle of the bean in ApplicationContext is basically the same as the Beanfactoty, and after the Beanfactoryaware interface adds a The Setapplicationcontext () method of the Applicationcontextaware interface.

The scope and life cycle of Spring beans

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.