InitialingBean, BeanPostProcessor, BeanFactoryPostProcessor

InitialingBean, BeanPostProcessor, BeanFactoryPostProcessor用法上的區別。

InitialingBean

接口實現類需要實現afterPropertiesSet方法,就像方法名,在對象的屬性值都設好之后才會調用。

void afterPropertiesSet() throws Exception;

BeanPostProcessor

如果配置了BeanPostProcessor,所有的bean初始化前后都會調用before方法和after方法。

Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;

方法的入參是bean和bean的名字,因此可以使用這個方法修改bean的屬性值等等。

BeanFactoryPostProcessor

實現改接口的類需要同時實現一下方法。

void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;

方法回調時間是所有的bean定義都被解析,但是還沒有一個bean被實例化,可以看到入參是beanFactory,因此可以對bean定義進行修改。也可以看出來該方法的回調時間是在BeanPostProcessor方法之前的。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容