Spring InitializingBean and DisposableBean example

本文介绍 Spring 框架中 InitializingBean 和 DisposableBean 接口的作用,通过示例展示如何使用这两个接口来实现 Bean 的初始化和销毁操作,并探讨了更好的实践方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction.

  1. For bean implemented InitializingBean, it will run afterPropertiesSet() after all bean properties have been set.
  2. For bean implemented DisposableBean, it will run destroy() after Spring container is released the bean.
Example
package org.kodejava.example.spring;
 
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
 
public class InitDisposeService
        implements InitializingBean, DisposableBean {
    
    /**
     * Do some processes.
     */
    public void doSomething() {
        System.out.println("InitDisposeService.doSomething");
    }
 
    /**
     * Initialize bean after property set.
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("InitDisposeService.afterPropertiesSet");
    }
 
    /**
     * Clean-up bean when the context is closed.
     *
     * @throws Exception
     */
    @Override
    public void destroy() throws Exception {
        System.out.println("InitDisposeService.destroy");
    }
}


 

Here’s an example to show you how to use InitializingBeanand DisposableBean. A CustomerService bean to implement both InitializingBean and DisposableBean interface, and has a message property.

 

File : Spring-Customer.xml

<?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="service" class="org.kodejava.example.spring.InitDisposeService"/>

</beans>


 

Run it

package org.kodejava.example.spring;
 
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InitDisposeDemo {
    public static void main(String[] args) {
        ConfigurableApplicationContext context =
                new ClassPathXmlApplicationContext("InitDispose.xml");
 
        InitDisposeService service =
                (InitDisposeService) context.getBean("service");
        service.doSomething();
 
        context.close();
    }
}


The ConfigurableApplicationContext.close() will close the application context, releasing all resources and destroying all cached singleton beans. It’s use fordestroy() method demo purpose only :)

 

Output

 
InitDisposeService.afterPropertiesSet
InitDisposeService.doSomething
InitDisposeService.destroy

 

The afterPropertiesSet() method is called, after the message property is set; while the destroy() method is call after the context.close();

 

Thoughts…
I would not recommend to use InitializingBean and DisposableBean interface, because it will tight coupled your code to Spring. A better approach should be specifying theinit-method and destroy-method attributes in your bean configuration file. see http://blog.youkuaiyun.com/gloomuu/article/details/8588380

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值