The Bean's life cycle in spring

Source: Internet
Author: User

Reference https://www.cnblogs.com/liran123/p/9409763.html

1, instantiation of a bean--is what we often say new;

2. Attribute injection to the bean according to the spring context

3, if the bean has implemented the Beannameaware interface, it will invoke its implementation of the Setbeanname (String) method, which is passed in the spring configuration file, the Bean ID value

4, if the bean has implemented the Beanfactoryaware interface, it will invoke its implementation of the Setbeanfactory (Setbeanfactory (beanfactory) The spring factory itself is passed (this is the way to get the other beans, just configure a normal bean in the Spring config file);

5, if the bean has implemented the Applicationcontextaware interface, it will call the Setapplicationcontext (ApplicationContext) method, Passing in the spring context (same way can also implement step 4 content, but better than 4, because ApplicationContext is beanfactory sub-interface, there are more implementation methods);

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 invokes its configured initialization method.

8. If the bean is associated with the Beanpostprocessor interface, the postprocessafterinitialization (Object obj, String s) method is called. If the bean implements the Initializingbean interface, Spring will invoke its Afterpropertiesset interface method.

Note: After the completion of the work can be applied to the bean, the Bean is a singleton, so generally we call the same ID of the bean will be in the same content address instance, of course, in the spring configuration file can also be configured non-singleton, Here we do not repeat.

9, when the bean is no longer needed, it will go through the cleanup phase, if the Bean implements Disposablebean this interface, will call that its implementation of the Destroy () method;

10. Finally, if the Destroy-method attribute is configured in the spring configuration of this bean, its configured destruction method is automatically invoked.

This describes the life cycle of applying the spring context bean, and if you apply Spring's factory, which is Beanfactory, remove the 5th step and OK.

The Bean's life cycle in spring

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.