spring bean创建之后和销毁之前

spring bean在创建之后和销毁之前的赋能,两种实现方式

通过注解的方式

@PostConstruct
    public void init(){
        System.out.println("service init");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("service destroy");
    }

通过实现接口

public abstract class AbstractService implements InitializingBean, DisposableBean {

    @Override
    public void afterPropertiesSet() throws Exception {

        System.out.println("AbstractService afterPropertiesSet");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("AbstractService destroy");
    }
}
posted @ 2020-08-31 14:22  0更新  阅读(131)  评论(0)    收藏  举报