For normal Java objects, when new creates an object, it is reclaimed by the garbage collection mechanism when it does not have any references. Objects that are managed by the spring IOC container have their lifecycle fully controlled by the container. The life cycle of each bean in spring is as follows:
1. Instantiating beans
For beanfactory containers, the container invokes Createbean for instantiation when the client requests an uninitialized bean from the container, or when the bean is initialized with another dependency that has not yet been initialized.
For the ApplicationContext container, all beans are instantiated when the container starts to finish.
The container is instantiated by getting the information in the Beandefinition object. And this step is just a simple instantiation, not a dependency injection.
Instantiated objects are wrapped in Beanwrapper objects, Beanwrapper provides an interface for setting object properties, thus avoiding the use of reflection mechanisms to set properties.
2. Setting object properties (Dependency injection)
The instantiated object is encapsulated in the Beanwrapper object, and at this point the object is still a native state, and no dependency injection is made.
Shortly thereafter, Spring relies on the information in the Beandefinition for dependency injection.
And the dependency injection is done through the interface of the settings property provided by Beanwrapper.
3. Inject Aware interface
Immediately, spring detects whether the object implements the Xxxaware interface and injects the associated Xxxaware instance to the bean.
4. Beanpostprocessor
After these steps, the Bean object has been properly constructed, but if you want to do some custom processing before the object is used, it can be implemented through the Beanpostprocessor interface.
The interface provides two functions:
Postprocessbeforeinitialzation (Object Bean, String beanname)
The Bean object that is currently being initialized is passed in and we can do any processing on the bean.
This function is executed prior to Initialzationbean and is therefore referred to as pre-processing.
The injection of all aware interfaces is done in this step.
Postprocessafterinitialzation (Object Bean, String beanname)
The Bean object that is currently being initialized is passed in and we can do any processing on the bean.
This function is executed after the Initialzationbean is completed and is therefore called post processing.
5. Initializingbean and Init-method
When the pre-processing of the beanpostprocessor is complete, it will enter this stage.
The Initializingbean interface has only one function:
This stage can also add our custom logic before the bean is formally constructed, but it differs from the predecessor processing because the function does not pass in the current Bean object, so there is no way to handle the object itself in this step, only adding some extra logic.
To use it, we need to have the bean implement the interface and write the added logic in the function. Spring then detects if the current bean implements the interface and executes the Afterpropertiesset function after the predecessor processing is complete.
Of course, in order to reduce the intrusion to the customer code, spring provides the Init-method property for the bean configuration, which specifies the name of the function that needs to be executed at this stage. Spring will execute the functions we set in the initialization phase. Init-method essentially still uses the Initializingbean interface.
6. Disposablebean and Destroy-method
As with Init-method, the specified logic can be executed before the bean is destroyed by assigning a function to the Destroy-method.
Deep analysis of the life cycle of Spring (iii)--bean