Mid-life of beans in spring with Initializingbean and Disposablebean interfaces

Source: Internet
Author: User

Spring provides a number of flag interfaces to change the behavior of beans in beanfactory. They include Initializingbean and Disposablebean. Implementing these interfaces will cause Beanfactory to invoke the Afterpropertiesset () method of the previous interface, invoking the latter interface Destroy () method, allowing the bean to do some specific actions after initialization and destruction.

Internally, Spring uses beanpostprocessors to handle the flags interfaces it can find and to invoke the appropriate method. If you need custom features or other life-cycle behaviors that spring does not provide, you can implement your own beanpostprocessor. More on this can be seen here: section 3.7, "Customizing Beans with Beanpostprocessors".

All the logo interfaces for the life cycle are described below. In the Appendix section, you can find diagrams that show how spring manages beans, how those life-cycle features change the nature of your beans, and how they are managed.

1. Initializingbean/init-method

Implementing the Org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all its required properties are beanfactory set. The Initializingbean interface only developed a method:

* Invoked by a beanfactory after it have set all bean properties supplied * (and satisfied beanfactoryaware and Applicat    Ioncontextaware). * <p>this method allows the bean instance to perform initialization if * possible when all bean properties has    been set and to throw a * exception in the event of misconfiguration. * @throws Exception in the event of misconfiguration (such * as failure to set a essential property) or if Initializat    Ion fails. */void Afterpropertiesset () throws Exception;

Note: Usually the use of the Initializingbean interface is avoidable (and discouraged, because it is not necessary to have the code coupled to spring). The bean definition supports specifying a common initialization method. With Xmlbeanfactory, you can do this by specifying the Init-method property. For example, the following definition:

<bean id= "Exampleinitbean" class= "examples. Examplebean "init-method=" Init "/>public class Examplebean {public void init () {//Do some initialization wo RK}}

Exactly like the following:

<bean id= "Exampleinitbean" class= "examples. Anotherexamplebean "/>public class Anotherexamplebean implements Initializingbean {public void Afterpropertiesset () {//Do some initialization}}

Instead of coupling the code to spring.

2. Disposablebean/destroy-method

Implementing the Org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the beanfactory containing it is destroyed. Disposablebean also specifies only one method:

/** * Invoked by a beanfactory on destruction of a singleton.    * @throws Exception in case of shutdown errors.    * Exceptions would get logged but not rethrown to allow * other beans to release their resources too. */void Destroy () throws Exception;

Note: The use of the Disposablebean interface is usually avoided (and is discouraged because it unnecessarily coupling the code to spring). The bean definition supports specifying a common destructor method. In the case of using xmlbeanfactory, it is done through the Destroy-method property. For example, the following definition:

<bean id= "Exampleinitbean" class= "examples. Examplebean "destroy-method=" destroy "/>public class Examplebean {public void Cleanup () {//Do some destruct Ion work (like closing connection)}}

Exactly like the following:

<bean id= "Exampleinitbean" class= "examples. Anotherexamplebean "/>public class Anotherexamplebean implements Disposablebean {public void Destroy () {//D O Some destruction work}}

Instead of coupling the code to spring.

Important tip: When deploying a bean in Portotype mode, the Bean's life cycle will change a little. By definition, spring cannot manage the entire life cycle of a non-singleton/prototype bean, because when it is created, it is handed over to the client and the container is no longer aware of it. When talking about non-singleton/prototype beans, you can think of Spring's character as a substitute for the "new" operator. Everything from then on to the life cycle is handled by the client. The life cycle of the bean in Beanfactory will be described in more detail in section 3.4.1 , "Life cycle Interfaces" .

Mid-life of beans in spring with Initializingbean and Disposablebean interfaces

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.