The life cycle of spring beans, the official version of the Spring combat book

Source: Internet
Author: User

A two-day interview was asked about the life cycle of the Spring bean, including a phone interview with Ali last night. Here we find the official version of spring in combat. I hope you want to interview the small partners remember, it is possible later, or there is time to see the source of the best (including myself). Ha ha

Bean's life cycle

In traditional Java applications, the bean's life cycle is simple. The bean is instantiated using the Java keyword New, and then the bean is ready for use. Once the bean is no longer in use, it is automatically garbage collected by Java.

In contrast, the bean in the Spring container has a relatively complex life cycle. It is important to understand the life cycle of the spring bean correctly, because you might want to use the extension points provided by spring to customize the bean creation process. Figure 1.5 shows a typical life-cycle process in which beans are loaded into the context of the spring application. (The phone shot.) )

Note that the bean has undergone several stages in the spring container from creation to destruction, each of which can be customized for how Spring manages beans

As you can see, the Bean Factory performs several boot steps before the bean is ready. We have a detailed description of Figure 1.5: ( Here is the need to back down )

1. Spring initializes the bean;

2. Spring injects values and bean references into the corresponding attributes of the bean;

3. If the bean implements the Beannameaware interface, Spring passes the Bean's ID to the Setbeanname () method;

4. If the bean implements the Beanfactoryaware interface, Spring calls the Setbeanfactory () method, passing the Beanfactory container instance in;

5. If the bean implements the Applicationcontextaware interface, Spring calls the Set-applicationcontext () method, passing in a reference to the application context where the bean resides;

6. If the bean implements the Beanpostprocessor interface, Spring will call their post-process-before-initialization () method;

7. If the bean implements the Initializingbean interface, Spring will call their After-properties-set () method. Similarly, if the bean declares the initialization method using Init-method, the method is also called;

8. If the bean implements the Beanpostprocessor interface, Spring will call their post-process-after-initialization () method;

9. At this point, the beans are ready to be used by the application, and they will reside in the application context until the application context is destroyed;

10. If the bean implements the Disposable-bean interface, Spring will call its destory () interface method. Similarly, if the bean declares the destroy method using Destory-method, the method is also called.

The above is what the book says, remember, in the interview can calmly deal with.

The life cycle of spring beans, the official version of the Spring combat book

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.