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