Spring InitializingBean and DisposableBean example

In Spring, InitializingBean and DisposableBean are two marker interfaces, a useful way for Spring to perform certain actions upon bean initialization and destruction.
For bean implemented InitializingBean, it will run after PropertiesSet() after all bean properties have been set.
For bean implemented DisposableBean, it will rundestroy() after Spring container is released the bean.

Example

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.

package com.mkyong.customer.services;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

public class CustomerService implements InitializingBean, DisposableBean{ 
    String message; 
    public String getMessage() { 
        return message; 
    } 
    public void setMessage(String message) { 
        this.message = message; 
     } 
    public void afterPropertiesSet() throws Exception { 
        System.out.println("Init method after properties are set : " + message); 
     } 
    public void destroy() throws Exception { 
        System.out.println("Spring Container is destroy! Customer clean up"); 
    } 
}

File : Spring-Customer.xml

<?xml version="1.0"?>
<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">
        <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. It’s use for destroy() method demo purpose only :)

Output

Init method after properties are set : im 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 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 the init-method and destroy-method attributes in your bean configuration file.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容