zoukankan      html  css  js  c++  java
  • Spring init-method and destroy-method example

    In Spring, you can use init-method and destroy-method as attribute in bean configuration file for bean to perform certain actions upon initialization and destruction. Alternative to InitializingBean and DisposableBean interface.

    Example

    Here’s an example to show you how to use init-method and destroy-method.

    package com.mkyong.customer.services;
    
    public class CustomerService
    {
    	String message;
    	
    	public String getMessage() {
    	  return message;
    	}
    
    	public void setMessage(String message) {
    	  this.message = message;
    	}
    	
    	public void initIt() throws Exception {
    	  System.out.println("Init method after properties are set : " + message);
    	}
    	
    	public void cleanUp() throws Exception {
    	  System.out.println("Spring Container is destroy! Customer clean up");
    	}
    	
    }
    

    File : Spring-Customer.xml, define init-method and destroy-method attribute in your bean.

    <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-2.5.xsd">
    
    	<bean id="customerService" class="com.mkyong.customer.services.CustomerService" 
    		init-method="initIt" destroy-method="cleanUp">
       		
    		<property name="message" value="i'm property message" />
    	</bean>
    		
    </beans>
    

    Run it

    package com.mkyong.common;
    
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.mkyong.customer.services.CustomerService;
    
    public class App 
    {
        public static void main( String[] args )
        {
        	ConfigurableApplicationContext context = 
    		new ClassPathXmlApplicationContext(new String[] {"Spring-Customer.xml"});
    	
        	CustomerService cust = (CustomerService)context.getBean("customerService");
        	
        	System.out.println(cust);
        	
        	context.close();
        }
    }
    

    The ConfigurableApplicationContext.close will close the application context, releasing all resources and destroying all cached singleton beans.

    Output

    Init method after properties are set : i'm property message
    com.mkyong.customer.services.CustomerService@47393f
    ...
    INFO: Destroying singletons in org.springframework.beans.factory.
    support.DefaultListableBeanFactory@77158a: 
    defining beans [customerService]; root of factory hierarchy
    Spring Container is destroy! Customer clean up
    

    The initIt() method is called, after the message property is set, and the cleanUp() method is called after the context.close();

    Thoughts…

    It’s always recommended to use init-method and destroy-method in bean configuration file, instead of implement the InitializingBean and DisposableBean interface to cause unnecessarily coupled your code to Spring.

  • 相关阅读:
    51nod1229 序列求和 V2
    51nod 1228、1258 序列求和
    题解P3711:【仓鼠的数学题】
    伯努利数学习笔记的说...
    题解 P4692 【[Ynoi2016]谁的梦】
    积性函数与卷积
    题解 P5065 【[Ynoi2014]不归之人与望眼欲穿的人们】
    [Ynoi2018]末日时在做什么?有没有空?可以来拯救吗?
    [51nod1965]奇怪的式子
    PGCD2
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4748387.html
Copyright © 2011-2022 走看看