@PostConstruct、@PreDestroy和initMethod、destroyMethod、InitializingBean、DisposableBean概念、区别以及执行顺序

@PostConstruct、@PreDestroy和initMethod、destroyMethod、InitializingBean、DisposableBean概念、区别以及执行顺序

@PostConstruct

定义相关

  1. 此注解是在Java EE5规范中加入的,不是spring的,spring是指遵循了这个规范,在Servlet生命周期中有一定作用,它通常都是一些初始化的操作,但初始化可能依赖于注入的其他组件,所以要等依赖全部加载完再执行
  2. @PostConstruct注解用于在依赖关系注入完成之后执行的方法
  3. 方法可能是用final修饰的

用法

在类中声明一个void方法,当然也可是是其他类型,但是没有实际意义,一般都是非静态void方法

 @PostConstruct
    public final  void initForPostConstruct(){
        System.out.println("initForPostConstruct 方法执行了!");
    }

@PreDestroy

定义相关

  1. 此注解也是是在Java EE5规范中加入的,不是spring的,spring是指遵循了这个规范在Servlet生命周期中有一定作用,它通常都是一些销毁前的一些操作。
  2. @PreDestroy 注解在方法上用作回调通知,以表明实例正在被容器删除
  3. 带有@PreDestroy 注解的方法通常用于释放它一直持有的资源,或做垃圾回收
  4. 方法可能是用final修饰的

用法

同@PostConstruct一样,在类中声明一个void方法,当然也可是是其他类型,但是没有实际意义,一般都是非静态void方法

 @PreDestroy
    public void destroyForPreDestroy(){
        System.out.println("destroyForPreDestroy 方法执行了!");
    }

initMethod和destroyMethod

定义相关

初始化一个对象(bean)后,立即初始化加载一些数据,和销毁对象前做一些资源释放、垃圾回收操作

用法

xml方式,在bean对应的类中定义init和destroy方法,并在xml中使用init-method和destroy-method关联

    public void initForInitMethod (){
        System.out.println("initForInitMethod 方法执行了!");
    }

    public void destroyFordestroyMethod(){
        System.out.println("destroyFordestroyMethod 方法执行了!");
    }
<?xml version="1.0" encoding="UTF-8"?>  
<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.xsd">  

<bean id="initOrDestroyTest" class="com.cyyit.bean.TestBean" init-method="init" destroy-method="destroy">
</bean>
</beans>

javaConfig方式,效果等同于xml方式

@Configuration
public class config {
    @Bean(initMethod = "initForInitMethod",destroyMethod ="destroyFordestroyMethod")
    public TestBean testBean(){
        return  new TestBean();
    }
}

InitializingBean

IinitializingBean 接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。

DisposableBean

DisposableBean 接口为bean提供了销毁Bean方法的方式,它只包括destroy方法,凡是继承该接口的类,在销毁bean的时候都会执行该方法。

执行顺序

测试:定义一个TestBean如下

public class TestBean implements InitializingBean, DisposableBean {
    private String name;
    private Integer age;
    @Autowired
    private TestBean1 testBean1;

    @PostConstruct
    public final  void initForPostConstruct(){
        System.out.println("initForPostConstruct 方法执行了!");
    }

    @PreDestroy
    public void destroyForPreDestroy(){
        System.out.println("destroyForPreDestroy 方法执行了!");
    }

    public void initForInitMethod (){
        System.out.println("initForInitMethod 方法执行了!");
    }

    public void destroyFordestroyMethod(){
        System.out.println("destroyFordestroyMethod 方法执行了!");
    }

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

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

执行main函数运行结果:
在这里插入图片描述
结论:由上可知,其实三种方法都是为了在springBean的生命周期中,能在初始化和销毁阶段做一些相关操作,三种方法其实用一种即可,实际中可根据需要配置,另外@PostConstruct、@PreDestroy是java自己的注解不是spring的,spring只是遵循了这样一个规范。三种执行顺序是:

初始化:@PostConstruct>InitializingBean>initMethod

销毁:@PreDestroy>DisposableBean>destroyMethod

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值