Spring(7)-Spring init-method and destroy-method example

本文介绍如何在Spring框架中使用init-method和destroy-method属性来定义bean的初始化和销毁方法,替代实现InitializingBean和DisposableBean接口。通过具体代码示例展示了如何配置这些方法,并解释了它们的工作原理。

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 toInitializingBean 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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值