Spring后置处理器(PostProcessor)

 

1.BeanPostProcessor


/**
* BeanPostProcessor为我们提供了两个方法,我们可以在Spring实例化Bean的前后实现我们的特定需求。
* (Spring AOP就是根据BeanPostProcessor建立与IOC的联系)
*/
public interface BeanPostProcessor {

    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

}
我们都知道,实现ApplicationContextAware接口,Spirng初始化Bean时会调用setApplicationContext方法,帮助我们注入ApplicationContext对象。其实这个功能就是利用BeanPostProcessor实现的,具体可以看源码参考Spirng的 ApplicationContextAwareProcessor这个类

 

2.BeanFactoryPostProcessor

/** 
* 实现该接口,可以在Spring的Bean创建之前修改Bean定义的属性。
*/

@FunctionalInterface
public interface BeanFactoryPostProcessor {

    void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

}

简单的说就是Spring提供了一个接口,这个接口可以让我们修改Spring内部的BeanFactory。(在这之前要请先了解什么是BeanFactory,并了解FactoryBean和BeanFactory的区别。

可参考:https://www.cnblogs.com/MichaelPL/p/11631808.html

 

 

 

 

 

BeanDefinitionRegistryPostProcessor
DestructionAwareBeanPostProcessor
InstantiationAwareBeanPostProcessor
MergedBeanDefinitionPostProcessor
RequiredAnnotationBeanPostProcessor

 

posted @ 2019-10-07 16:45  Suker-Liang  阅读(1131)  评论(0编辑  收藏  举报