spring源码分析-DisposableBean、destroyMethod和@PreDestroy

227 阅读1分钟

DisposableBean

  • 实现DisposableBean接口,重写destroy方法,作用在bean销毁之前

destroyMethod

  • @Bean注入对象时,通过@Bean(destroyMethod= “destroyMethod”)的方式,使当前bean中的destroyMethod方法在bean销毁之前被调用

@PreDestroy

  • JSR250定义的java规范,使用方法:直接在类中的某个方法上加上该注解,使该方法在bean销毁前被调用

InitializingBean,initMethod,@PostConstruct

InitializingBean

  • 使用方法:实现InitializingBean接口,重写afterPropertiesSet方法,作用在bean初始化过程中

initMethod

  • @Bean注入对象时,通过@Bean(initMethod = “init”)的方式,使当前bean中的init方法在bean初始化过程中被调用

@PostConstruct

  • JSR250定义的java规范,使用方法:直接在类中的某个方法上加上该注解,使该方法在bean初始化时被调用