Spring learning notes about Bean initialization and cleanup-init and destroy Methods

Source: Internet
Author: User
Tags xml example

If you need to perform specific initialization and cleanup operations when the bean is created or destroyed, there are two options in spring.

1. Define the init-method and destroy-method attributes of bean in XML.


Execute the init and destroy Methods unforcibly, and configure them freely in XML. The dependency with the Spring framework is low.

2. Enable the bean initialization and destruction operation interfaces initializingbean and disposablebean, such:

public class Message  implements InitializingBean , DisposableBean 

Implement the corresponding interface to enforce the initialization or destruction method, but it will increase the dependency between the Code and the Spring framework.

 

Note that the destroy method is executed only when the scope attribute of bean is singleton and the bean is created in singleton mode. The destroy of bean is managed by springcontainer.

Xml configuration example

Spring-config.xml // The destroy method is not executed if scope = "prototype" is added to the bean: messageloaders definition in the following configuration, that is, the destroy method is not created using Singleton mode.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">     <bean id="messageLooper" class="initanddestroy.MessageLooper" init-method="init" destroy-method="destroy" />    </beans>

Messagelotify. Java // class with the init and destroy Methods

Package initanddestroy; public class messageloaders {// destroy method public void destroy () {system. out. println ("Destroy executed");} // initialization method public void Init () {system. out. println ("init executed ");}}

Main method class:

Initanddestroymain. Java

Package initanddestroy; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import initanddestroy. messagelotify; public class initanddestroymain {/***** @ comment [note] * @ author ronglei, 2012-7-24 *** @ Param ARGs */public static void main (string [] ARGs) {string configstring = "initanddestroy/spring-config.xml"; applicationcontext context = new classpathxmlapplicationcontext (configstring); messagelow.messagelow.context. getbean ("messageloaders", messageloaders. class); // close the context to trigger the bean destruction method (classpathxmlapplicationcontext) context ). close ();}}

Output:

Example of initializingbean and disposablebean Interface Configuration

Spring-config.xml // Similarly, if messageloaders defines the scope = "" value at Bean timing and is not Singleton, the destroy method will not be executed.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">        <bean id="messageLooper" class="initanddestroy.MessageLooper"  /> </beans>

Messagelotify. Java code:

package initanddestroy;import org.springframework.beans.factory.DisposableBean;import org.springframework.beans.factory.InitializingBean;public class MessageLooper  implements InitializingBean , DisposableBean {    @Override    public void destroy() throws Exception    {        System.out.println("destroy executed");    }    @Override    public void afterPropertiesSet() throws Exception    {        System.out.println("init executed");      }}

Main method class: // In fact, this main method class is the same as the code in the preceding XML example.

Package initanddestroy; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext; import initanddestroy. messagelotify; public class initanddestroymain {/***** @ comment [note] * @ author ronglei, 2012-7-24 *** @ Param ARGs */public static void main (string [] ARGs) {string configstring = "initanddestroy/spring-config.xml"; applicationcontext context = new classpathxmlapplicationcontext (configstring); messagelow.messagelow.context. getbean ("messageloaders", messageloaders. class); // close the context to trigger the bean destruction method (classpathxmlapplicationcontext) context ). close ();}}

Execution result:

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.