在spring的init-method指定的方法之前做一些事情要用BeanPostProcessor

package com.test.spring.beans;


import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Cashier implements StorageConfig {


    private String name;
    private String path;
    private BufferedWriter writer;
    public Cashier() {
        System.out.println("*****");
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
    
    public void openFile() throws FileNotFoundException {
        System.out.println("openfile--->");
        File logFile = new File(path, name + ".txt");
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(logFile, true)));
    }
    public void checkout(ShoppingCart cart) throws IOException {
        double total =0;
        for(Product product: cart.getItems()) {
            total += product.getPrice();
        }
        writer.write(new Date() + "\t" + total + "\r\n");
        writer.flush();
    }
    public void closeFile() throws IOException {
        System.out.println("close---->");
        writer.close();
    }
    public static void main(String args[]) throws IOException {
        ApplicationContext appContext = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        Cashier c = (Cashier) appContext.getBean("cashier");
        Product p = new Product("aa",11.1);
        List<Product> ps = new ArrayList<Product>();
        ps.add(p);
        ShoppingCart sc = new ShoppingCart();
        sc.setItems(ps);
        c.checkout(sc);
        System.out.println("checkout---->");
    }
    @Override
    public String getpath() {
        // TODO Auto-generated method stub
        return path;
    }

}



package com.test.spring.beans;


public interface StorageConfig {


    public String getpath();
}



package com.test.spring.beans;


import java.io.File;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;


public class PathCheckingBeanPostProcessor implements BeanPostProcessor {


    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        // TODO Auto-generated method stub
        return bean;
    }


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("post---->");
        if(bean instanceof StorageConfig) {
           
            String path = ((StorageConfig)bean).getpath();
            File file = new File(path);
            if(!file.exists()) {
                file.mkdirs();
            }
        }
        return bean;
    }
    
    
}

这样实现了将所有的bean实例都传递给bean的后处理器,所以要检查bean是不是storageconfig的实例,进行过滤判断。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值