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方法之前的。