/*** 指定组建的init方法和destroy的几种方法* 1:在配置类中 @Bean(initMethod = "init",destroyMethod = "destory")注解指定* 2:实现InitializingBean重写其afterPropertiesSet方法,重写DisposableBean重写destroy方法* 3:利用java的JSR250规范中的@PostConstruct标注在init方法上,@PreDestroy标注在destroy注解上*/
bean实现了InitializingBean接口并重写他的afterPropertiesSet方法,就相当于为bean指定了init-method,会在bean赋值完参数之后执行该方法
bean实现了DisposableBean接口并重写了他的destroy方法,就相当于为bean指定了destroy-method,会在bean销毁的时候被调用
下面是具体的代码
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;public class Cat implements InitializingBean,DisposableBean {@Overridepublic void destroy() throws Exception {System.out.println("Cat...destroy");}@Overridepublic void afterPropertiesSet() throws Exception {System.out.println("Cat...afterPropertiesSet");}
}