zoukankan      html  css  js  c++  java
  • Spring Bean InitializingBean和DisposableBean实例

    在Spring中,InitializingBean和DisposableBean是两个标记接口,为Spring执行时bean的初始化和销毁某些行为时的有用方法。
    1. 对于Bean实现 InitializingBean,它将运行 afterPropertiesSet()在所有的 bean 属性被设置之后。
    2. 对于 Bean 实现了DisposableBean,它将运行 destroy()在 Spring 容器释放该 bean 之后。

    示例

    下面是一个例子,向您展示如何使用 InitializingBean 和 DisposableBean。一个 CustomerService bean来实现 InitializingBean和DisposableBean 接口,并有一个消息(message)属性。
    package com.yiibai.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 : applicationContext.xml

    <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.yiibai.customer.services.CustomerService">
    		<property name="message" value="I'm property message" />
           </bean>
    		
    </beans>

    执行它

    package com.yiibai.common;
    
    import org.springframework.context.ConfigurableApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import com.yiibai.customer.services.CustomerService;
    
    public class App 
    {
        public static void main( String[] args )
        {
        	ConfigurableApplicationContext context = 
    			new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
    	
        	CustomerService cust = (CustomerService)context.getBean("customerService");
        	
        	System.out.println(cust);
        	
        	context.close();
        }
    }
    该ConfigurableApplicationContext.close()将关闭该应用程序的上下文,释放所有资源,并销毁所有缓存的单例bean。它是只用于 destroy() 方的演示目的。

    输出结果

    Init method after properties are set : I'm property message 
    com.yiibai.customer.services.CustomerService@4090c06f 
    Spring Container is destroy! Customer clean up
    afterPropertiesSet()方法被调用在 message 属性设置后,而 destroy()方法是在调用 context.close()之后;
     
    建议:
    不建议使用InitializingBean和DisposableBean的接口,因为它将你的代码紧耦合到 Spring 代码中。 一个更好的做法应该是在bean的配置文件属性指定 init-method和destroy-method
     
  • 相关阅读:
    sdnu 1513 字符串翻转
    hdu-1559 最大子矩阵(二维树状数组模板题)
    hdu-1556 树状数组
    1049.饭盒
    1092.校门外的树
    1012.区间合并
    1054.数独
    1175.开心的金明 01背包
    空格(Space)的ASCII码值是:32
    js为lable和div赋值
  • 原文地址:https://www.cnblogs.com/soundcode/p/6367410.html
Copyright © 2011-2022 走看看