Spring Bean的生命周期(三)

生命周期

  • 定义
  • 初始化
  • 使用
  • 销毁

方法

初始化、销毁

  1. 实现接口InitializingBean, DisposableBean
public class BeanLifeCycle implements InitializingBean, DisposableBean{
@Override
    public void destroy() throws Exception {
        System.out.println("Bean destroy.");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Bean afterPropertiesSet.");
    }
}
  1. xml配置 init-method="start" destroy-method="stop"
 <bean id="beanLifeCycle" class="com.imooc.lifecycle.BeanLifeCycle"  init-method="start" destroy-method="stop"></bean>

default-init-method="defautInit" default-destroy-method="defaultDestroy"

    public void defautInit() {
        System.out.println("Bean defautInit.");
    }
    
    public void defaultDestroy() {
        System.out.println("Bean defaultDestroy.");
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容