Always think of the spring bean as a servlet.
The life cycle of a servlet:Instantiation (Singleton)--Initialize init-
--Service
request--Destroy destroy
the life cycle of the spring context Bean:
Instantiate a bean--new
Configuring beans based on the spring context--IOC injection
When the bean implements the Beannameaware interface, it calls the Setbeanname (String) method, passing the ID of the bean in a spring configuration file
When the bean implements the Beanfactoryaware interface, calls the Setbeanfactory method, passes the spring factory itself, and configures it by simply configuring a normal bean inside the bean. After the use of this
If the bean already implements the Applicationcontextaware interface, the Setapplicationcontext (ApplicationContext) method is called, Passing in the spring context (same way you can implement step 4, but better than 4, because ApplicationContext is Beanfactory's sub-interface, there are more implementations)
If the bean is associated with the Beanpostprocessor interface, the postprocessbeforeinitialization (Object obj, String s) method is called, Beanpostprocessor is often used as a change to the bean content, and because it is called at the end of the bean initialization process, it can also be applied to memory or cache technology
If the bean is configured in the spring configuration file, the Init-method property automatically calls its configured initialization method
Getting Started with beans
Bean destruction
When using the Spring Factory (beanfactory) to remove 5
The Bean's life cycle in the spring framework
When a bean is incorporated into the spring IOC container, the bean's life cycle is managed entirely by the container, when the manager is beanfactory:
- bean creation --Read the bean definition file by beanfactory, generate each instance
- setter injection --to execute the Bean's property dependency injection
- setbeanname () --if implementing the Beannameaware interface
- setbeanfactory () --if implementing Beanfactoryaware interface
- processbeforeinitialization () -- If there is an instance that implements the Beanpostprocessors interface, execute the Processbeforeinitialization () method of the interface
- afterpropertiesset ()-- If the Bean class implements the Org.springframework.beans.factory.InitializingBean interface, it executes its afterpropertiesset () method
- define the bean Init-method--
8.processafterinitialization ()-- If there is an instance of the Org.springframework.beans.factory.BeanPostProcessors interface implemented in the container, any bean will perform the processafterinitialization of the instance before it is initialized () method.
9.Destroy ()--If the Bean class implements the Org.springframework.beans.factory.DisposableBean interface, it executes its destroy () method.
Destroy-method--the method defined by "Destory-method" can be used in the bean definition file when the container is closed
The life cycle of spring beans