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相关注解的,打上断点,查看堆栈



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

被折叠的 条评论
为什么被折叠?



