玖叶教程网

前端编程开发入门

InitializingBean/DisposableBean


package com.itheima.initialize;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

@Component
public class Animal implements InitializingBean, DisposableBean {
    //PostConstructor,PreDestroy, 提供初始化方法和销毁方法的
    @PostConstruct
    public void initMethod(){
        System.out.println("Animal...PostConstruct..initMethod");
    }

    @PreDestroy
    public void destroyMethod(){
        System.out.println("Animal...PreDestroy..destroyMethod");
    }
    //销毁方法
    @Override
    public void destroy() throws Exception {
        System.out.println("Animal...destroy...");
    }

    //初始化方法
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Animal...afterPropertiesSet...");
    }
}

实现初始化和销毁接口后,继承的方法会按时调用。

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言