---
title: "Spring - Initializing and destroying beans"
author: "weini174"
date: "2013-01-21T11:55:47Z"
tags: []
categories: ["企业架构"]
slug: "spring-initializing-and-destroying-beans"
draft: false
lastmod: "2013-01-21T11:55:47Z"
id: 1773000
url: "https://weini174.iteye.com/blog/1773000"
---

1\. 使用init-method和destroy-method属性：

&lt;bean id="kenny" class="com.springinaction.springidol.Instrumentalist"  
init-method="tuneInstrument"  
destroy-method="cleanInstrument"&gt;

    &lt;property name="song" value="Jingle Bells" /&gt;

  &lt;property name="instrument" ref="saxophone" /&gt;

&lt;/bean&gt;

 

2\. 使用default-init-method and default-destroy-method属性：

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)  
[http://www.springframework.org/schema/beans/spring-beans-2.0.xsd](http://www.springframework.org/schema/beans/spring-beans-2.0.xsd)"  
default-init-method="tuneInstrument"  
default-destroy-method="cleanInstrument"&gt; ...  
&lt;/beans&gt;

 

3\. 实现InitializingBean and DisposableBean():

InitializingBean.afterPropertiesSet()

DisposableBean.destroy()