BeanPostProcessor是spring提供的一個(gè)擴(kuò)展點(diǎn),通過BeanPostProcessor擴(kuò)展點(diǎn),我們可以對(duì)Spring管理的bean進(jìn)行再加工。比如:修改bean的屬性(@ConfigurationProperties注解的原理)、生成一個(gè)動(dòng)態(tài)代理(事物)等。
public interface BeanPostProcessor {
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
它有兩個(gè)方法postProcessBeforeInitialization,postProcessAfterInitialization。
分別在bean初始化之前和之后執(zhí)行。
執(zhí)行順序如下:
1,首先執(zhí)行bean的構(gòu)造方法,
2,BeanPostProcessor的postProcessBeforeInitialization方法
3,InitializingBean的afterPropertiesSet方法
4,initMethod方法
5,BeanPostProcessor的postProcessAfterInitialization方法