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

一. 三种方法概述

  1. 在配置类中指定 @Bean(initMethod = “init”,destroyMethod = “destory”)注解
  2. 实现InitializingBean接口并重写其afterPropertiesSet方法,实现DisposableBean接口并重写destroy方法
  3. 利用java的JSR250规范中的@PostConstruct标注在init方法上,@PreDestroy标注在destroy方法上

二. 方法详述

1. 方法1:配置类中指定

  1. 示例代码
public class CarA {
    public CarA() {
        System.out.println("CarA。。。构造函数");
    }

    public void initCarA(){
        System.out.println("CarA的init()方法");
    }

    public void destroyCarA(){
        System.out.println("CarA的destroy()方法");
    }
}

@Configuration
public class ConfigTest {
    @Bean(initMethod = "initCarA",destroyMethod = "destroyCarA")
    public CarA carA(){
        return new CarA();
    }
}
  1. 执行结果
CarA。。。构造函数
CarA的init()方法

服务启动

CarA的destroy()方法

2. 方法2:实现接口并重写方法

2.1 示例代码
public class CarB implements InitializingBean, DisposableBean {
    public CarB() {
        System.out.println("CarB。。。构造函数");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("CarB。。。afterPropertiesSet()方法执行");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("CarB。。。destroy()方法执行");
    }
}

@Configuration
public class ConfigTest {
    @Bean
    public CarB carB(){
        return new CarB();
    }
}
  • 执行结果
CarB。。。构造函数
CarB。。。afterPropertiesSet()方法执行

服务启动

CarB。。。destroy()方法执行
2.2 概述
  1. Spring 开放了扩展接口,允许我们自定义 bean 的初始化和销毁方法。即当 Spring 容器在 bean 进行到相应的生命周期阶段时,会自动调用我们自定义的初始化和销毁方法。这两个扩展接口是 InitializingBean 和 DisposableBean 。
  2. InitializingBean 接口说明:该接口为 bean 提供了 bean 属性初始化后的处理方法,它只有 afterPropertiesSet 一个方法,凡是实现此接口的类,在 bean 的属性初始化后都会执行该方法。
package org.springframework.beans.factory;
public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}
  1. DisposableBean 接口说明:该接口为单例 bean 提供了在容器销毁 bean 时的处理方法,它只有 destroy 一个方法,凡是实现此接口的类,在 bean 被销毁时都会执行该方法。
package org.springframework.beans.factory;
public interface DisposableBean {
    void destroy() throws Exception;
}
2.3 方法1 && 方法2
  1. 相同点:都是在 bean 属性初始化之后需要执行的初始化方法。
  2. 不同点
    1. 方法1:代码不与Spring耦合;执行效率较低(通过反射来执行initMethod 方法)
    2. 方法2:代码与Spring紧耦合;速度更快(将 bean 强制转换成 InitializingBean 接口类型,然后直接调用 afterPropertiesSet 方法)
  3. 说明:afterPropertiesSet 和 initMethod 可以同时存在,但是 afterPropertiesSet 方法是在 initMethod 方法之前执行的。
  4. 一个 bean 从创建到初始化的过程总结
    1. 通过构造器创建 bean
    2. 属性注入
    3. 执行 afterPropertiesSet 方法
    4. 执行 initMethod 方法

3. 方法3:利用java的JSR250规范

  1. 代码示例
public class CarC {

    public CarC() {
        System.out.println("CarC。。。构造函数");
    }

    @PostConstruct
    public void initCarC(){
        System.out.println("CarC。。。初始化方法initCarC()");
    }

    @PreDestroy
    public void destroyCarC(){
        System.out.println("CarC。。。销毁方法destroyCarC");
    }
}

@Configuration
public class ConfigTest {
    @Bean
    public CarC carC(){
        return new CarC();
    }
}
  1. 执行结果
CarC。。。构造函数
CarC。。。初始化方法initCarC()

服务启动

CarC。。。销毁方法destroyCarC

参考资料

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值