The life cycle of spring beans

Source: Internet
Author: User

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:
  1. Instantiate a bean--new

  2. Configuring beans based on the spring context--IOC injection

  3. When the bean implements the Beannameaware interface, it calls the Setbeanname (String) method, passing the ID of the bean in a spring configuration file

  4. 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

  5. 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)

  6. 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

  7. If the bean is configured in the spring configuration file, the Init-method property automatically calls its configured initialization method

  8. Getting Started with beans

  9. 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:

    1. bean creation --Read the bean definition file by beanfactory, generate each instance
    2. setter injection --to execute the Bean's property dependency injection
    3. setbeanname () --if implementing the Beannameaware interface
    4. setbeanfactory () --if implementing Beanfactoryaware interface
    5. processbeforeinitialization () -- If there is an instance that implements the Beanpostprocessors interface, execute the Processbeforeinitialization () method of the interface
    6. afterpropertiesset ()-- If the Bean class implements the Org.springframework.beans.factory.InitializingBean interface, it executes its afterpropertiesset () method
    7. 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

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.