The life cycle of the "Go" Spring bean

Source: Internet
Author: User

Spring Bean life cycle
In traditional Java applications, the Bean life cycle is very simple.
Java's keyword new is used to instantiate a bean (perhaps he is a non-serialized). That's enough.
Instead, the bean's life cycle is more granular in the spring container.
Understanding the life cycle of the spring bean is important because you might want to take advantage of the opportunity provided by spring to customize the bean creation process.

1. The container looks for the bean's definition information and instantiates it.

2. With dependency injection, spring configures all of the bean's properties according to the bean definition information.

3. If the bean implements the Beannameaware interface, the factory calls the Bean's Setbeanname () method to pass the Bean's ID.

4. If the bean implements the Beanfactoryaware interface, the factory calls the Setbeanfactory () method into the factory itself.

5. If beanpostprocessor is associated with a bean, then their postprocessbeforeinitialzation () method is called.

6. If the bean specifies the Init-method method, it will be called.

7. Finally, if there are beanpsotprocessor and bean associations, then their postprocessafterinitialization () method will be called.
By this time, the bean can already be used by the application system and will be kept in the bean factory to know it is no longer needed.

There are two ways to remove it from the Bean factory.

1. If the bean implements the Disposablebean interface, the Destory () method is called.

2. If a custom destroy method is specified, this method is called.

The life cycle of the bean in the spring application context is only a little different from the life cycle in The Bean factory,
The only difference is that if the bean implements the Applicationcontextawre interface, the Setapplicationcontext () method is called.

Only beans with singleton behavior accept the container management life cycle.
The bean,spring container for Non-singleton behavior is simply the replacement of new, and the container is only responsible for creation.


For singleton bean,spring containers know when a bean is instantiated and when it is destroyed,
Spring can manage the behavior of the end of the instantiation and before the destruction, managing the bean's life cycle behavior without the following two opportunities:
After the bean all depends on injection
Before the bean is about to be destroyed

1) Behavior implementation after dependency injection:
There are two methods: A. Writing the Init method B. Implementing the Initializingbean interface
Afterpropertiesset and Init appear at the same time, the former executes before the latter, using the Init method, you need to add the Init-method property to the configuration file

2) behavior before the bean is destroyed
There are two ways to do this: a. Writing the Close Method B. Implementing the Disposablebean interface
Destroy and close appear at the same time, prior to the latter execution, using the Close method, you need to add the Destroy-method property to the configuration file

The life cycle of the "Go" Spring bean

Related Article

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.