spring生命周期-BeanPostProcessor在spring中应用

本文深入探讨了Spring框架中BeanPostProcessor接口的实现类,包括ApplicationContextAwareProcessor如何注入applicationContext,以及InitDestroyAnnotationBeanPostProcessor如何处理@PostConstruct和@PreDestroy注解。

BeanPostProcessor 接口在spring中的实现类比较多,挑选几个比较重要的了解下

1.ApplicationContextAwareProcessor

可以通过实现ApplicationContextAware 接口,给bean里面注入applicationContext ;

@Component
public class Dog implements ApplicationContextAware {
    private ApplicationContext applicationContext ;

    public Dog(){
        System.out.println("dog constructor...");
    }

    //对象创建并赋值之后调用
    @PostConstruct
    public void init(){
        System.out.println("Dog....@PostConstruct...");
    }

    //容器移除对象之前
    @PreDestroy
    public void detory(){
        System.out.println("Dog....@PreDestroy...");
    }


    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}

实现逻辑在
在这里插入图片描述
在这里插入图片描述

2.InitDestroyAnnotationBeanPostProcessor

此后置处理器主要是处理@PostConstruct和@PreDestroy相关注解的,打上断点,查看堆栈

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值