初始化Bean和销毁Bean的三种方法

第一种方法,继承InitializingBean和DisposableBean接口
前者实现afterPropertiesSet方法,后者实现destroy方法,
代码示例:
定义一个实体类:

public class Info {
}

定义另一个实体类实现InitializingBean和DisposableBean接口:

public class Bus implements InitializingBean,DisposableBean {

    private Info info;

    public void setInfo(Info info) {
        System.out.println("setInfo");
        this.info = info;
    }

    public Info getInfo() {
        return info;
    }

    public Bus(){
        System.out.println("bus constr");
    }

    //InitializingBean的方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("==============afterPropertiesSet==========");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("=================destroy===================");
    }
}

配置类:

@Configuration
public class AppContext {

    @Bean
    public Info info(){
        return new Info();
    }

    @Bean
    public Bus createBus(){
        Bus bus = new Bus();
        bus.setInfo(info());
        return bus;
    }
}

测试类:

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppContext.class);
        Bus bus = context.getBean(Bus.class);
        System.out.println(bus);
        context.close();
    }
}

测试结果:

bus constr
setInfo
==============afterPropertiesSet==========
com.zhihao.miao.bean.demo4.Bus@73d4cc9e
=================destroy===================

我们发现继承InitializingBean接口实现的afterPropertiesSet方法在属性设置之后执行,继承DisposableBean接口实现destroy方法在对象销毁之后执行。

InitializingBean接口

Interface to be implemented by beans that need to react once all their properties have been set by a BeanFactory: for example, to perform custom initialization, or merely to check that all mandatory properties have been set.
这个接口的实现在所有属性被BeanFactory设置之后才被执行:比如说。执行自定义的初始化或者检查虽有必要的属性是否被设置。

An alternative to implementing InitializingBean is specifying a custom init-method, for example in an XML bean definition. For a list of all bean lifecycle methods, see the BeanFactory javadocs.
实现InitializingBean的替代方法是指定一个自定义的init方法,例如在一个XML bean定义中。 有关所有bean生命周期的方法,请参阅BeanFactory 帮助文档

DisposableBean接口

Interface to be implemented by beans that want to release resources on destruction. A BeanFactory is supposed to invoke the destroy method if it disposes a cached singleton. An application context is supposed to dispose all of its singletons on close.
这个接口的实现在bean实例被销毁的时候释放资源被调用。BeanFactory支持调用destroy方法处理缓存单列。

An alternative to implementing DisposableBean is specifying a custom destroy-method, for example in an XML bean definition. For a list of all bean lifecycle methods, see the BeanFactory javadocs.
实现DisposableBean的替代方法是指定一个自定义的destroy方法,例如在XML bean定义。 有关所有bean生命周期方法,请参阅BeanFactory 帮组文档。

第二种方式使用@Bean注解的参数
定义一个实例类,并在实例类中定义初始化方法和销毁方法

public class Bike {
    public void init(){
        System.out.println("=== bike ===init ====");
    }

    public void destroy() {
        System.out.println("===== bike ===destroy ====");
    }

}

定义一个AppContext类,用@Configuration注解标记一下

@Configuration
public class AppContext {

    @Bean(initMethod="init",destroyMethod="destroy")
    public Bike createBike(){
        return new Bike();
    }
}

测试:

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppContext.class);
        System.out.println(context.getBean(Bike.class));
        context.close();
    }
}

执行结果:

=== bike ===init ====
com.zhihao.miao.bean.demo5.Bike@68ceda24
===== bike ===destroy ====

第三种使用@PostConstruct和@PreDestroy注解
定义一个实体类:

public class Train {

    @PostConstruct
    public void initial(){
        System.out.println("......initial......");
    }

    @PreDestroy
    public void close(){
        System.out.println("......close.........");
    }

}

定义一个实体类,配置了@Configuration注解:

@Configuration
public class AppContext {

    @Bean
    public Train createTrain(){
        return new Train();
    }

}

测试:

public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppContext.class);
        System.out.println(context.getBean(Train.class));
        context.close();
    }
}

测试结果:

......initial......
com.zhihao.miao.bean.demo6.Train@4516af24
......close.........



 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值