参考 https://www.jianshu.com/p/2692bf784976
https://blog.csdn.net/asdfsadfasdfsa/article/details/71192476 简单用法
以下为应用
https://www.jianshu.com/p/3d099ea43b0e
https://www.jianshu.com/p/1417eefd2ab1
https://guoliangqi.iteye.com/blog/635826
https://www.cnblogs.com/wangpeng-friend/p/6247918.html
主要功能 | 调用时机 | 接口实现类/子类及常见应用场景 | |
BeanFactoryPostProcessor | 容器级别的扩展。 | beanFactory完成了标准初始化流程,且所有definitions已被加载 ,但是还没有开始实例化bean | PropertyPlaceholderConfigurer——用来替换配置文件的变量 CustomEditorConfigurer——解析一些特殊类型的属性,例如Date类型 |
BeanPostProcessor | Bean扩展 | Bean实例化之后,会在初始化前后调用(即initializeBean方法中),有下面连个方法: postProcessBeforeInitialization ——实例化前调用 |
|
XXXXXAware | 一般用来获取容器,例如获取BeanFactory, 获取AppplicationContext | BeanFactory是在bean初始化期间调用, 但ApplicationContext是在初始化之前调用。 因为ApplicationContext 是借助ApplicationContextAwareProcessor 的postProcessBeforeInitialization实现的 | BeanFactoryAware ,ApplicationContextAware ApplicationEventPublisherAware等等 |
InstantiationAwareBeanPostProcessor | 是一种特殊的BeanPostProcessor | 在bean实例化之前即调用(而不是初始化前后),返回代理bean | AOP原理就是通过实现 InstantiationAwareBeanPostProcesso 接口来返回代理对象的 |
以上扩展接口,多数都会在ApplicationContext启动期间注册,在getBean()时调用,
其中容器启动时调用了一个非常重要的方法叫refresh(),从中可以清楚看到大多数扩展接口的注册
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}