Spring笔记:定制Bean的初始化和销毁过程

描述:在开始过程中,组件在使用之前需要执行一些特定的初始任务,比如打开文件,连接数据库等。在结束它们的生命周期时,也需要执行与之对应的销毁任务。在Spring IoC容器里,除了能够注册组件外,还能管理组件的生命周期,Spring允许Bean生命周期的特定点执行任务。

Spring IoC容器对Bean生命周期进行管理的过程:

1)通过构造器或工厂方法创建Bean的实例

2)为Bean的属性设置值和对应的Bean引用

3)调用Bean的初始化回调方法

4)使用Bean

5)当容器关闭时,调用Bean的销毁回调方法

三种实现方法:

一、组件实现InitializingBean和DisposableBean接口,在aferPropertiesSet()和destroy()分别调用openFile()和closeFile()方法。

二、在Bean的声明中设置init-method和destroy-method属性,为Bean的初始化和销毁的回调方法。

三、使用注解,在初始化回调方法添加生命注解@PostConstruct,在销毁回调方法添加生命注解@PreDestroy。

例子:商店结账功能:

Cashier类:用于检查购物车的商品,在文本文件中记录每次结账的日期和金额

package chapter4_2;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import chapter4_2.Produce;
import chapter4_2.ShoppingCart;

public class Cashier implements InitializingBean,DisposableBean {

    private String name;
    
    private String path;
    
    private BufferedWriter writer;

    public void setName(String name) {
        this.name = name;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setWriter(BufferedWriter writer) {
        this.writer = writer;
    }
    
    //打开文件
    public void openFile()throws Exception
    {
        File logFile = new File(path,name+".txt");
        
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFile,true)));
        
    }
    //写入日志
    public void checkOut(ShoppingCart cart1) throws Exception
    {
        double total = 0;
        for(Produce p:cart1.getItems())
        {
            total += p.getPrice();            
        }
        writer.write(new Date() +"\t"+total+"\r\n");
        writer.flush();
    }
    //关闭
    public void closeFile()throws Exception
    {
        writer.close();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        openFile();        
    }

    @Override
    public void destroy() throws Exception {
        closeFile();
    }
}

 

配置文件:

<bean id="aaa" class="chapter4_2.Battery">
     <property name="name" value="aaa"></property>
     <property name="price" value="22"></property>
</bean> <bean id="disc" class="chapter4_2.Disc"> <property name="name" value="Disc"></property> <property name="price" value="555"></property> </bean> <bean id="shoppingCart" class="chapter4_2.ShoppingCart" scope="prototype"> </bean> <bean id="cashier" class="chapter4_2.Cashier"> <property name="name" value="ccc"></property> <property name="path" value="E:\cashier"></property> </bean>

测试方法:

public static void main(String[] args)throws Exception {
        
        ApplicationContext context = new ClassPathXmlApplicationContext("app4.xml");
        
        Produce aaa = (Produce)context.getBean("aaa");
        
        Produce disc = (Produce)context.getBean("disc");
        
        ShoppingCart cart1 = (ShoppingCart)context.getBean("shoppingCart");
        
        cart1.addItem(aaa);
        cart1.addItem(disc);
        
        Cashier c = (Cashier)context.getBean("cashier");        
        c.checkOut(cart1);
    }

 

 

 

 

posted @ 2013-04-03 21:01  爱生活者wmmang  Views(384)  Comments(0Edit  收藏  举报