
BeanPostProcessor
關於對象初始化前后的回調。
public interface BeanPostProcessor {
//該方法在bean實例化完畢(且已經注入完畢),在afterPropertiesSet或自定義init方法執行之前
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
//在afterPropertiesSet或自定義init方法執行之后
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
InstantiationAwareBeanPostProcessor
關於對象實例化前后以及實例化后設置propertyValues的回調
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
//這個方法用來在對象實例化前直接返回一個對象(如代理對象)來代替通過內置的實例化流程創建對象;
@Nullable
default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
//在對象實例化完畢執行populateBean之前 如果返回false則spring不再對對應的bean實例進行自動依賴注入。
default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
return true;
}
//這里是在spring處理完默認的成員屬性,應用到指定的bean之前進行回調,可以用來檢查和修改屬性,最終返回的PropertyValues會應用到bean中
//@Autowired、@Resource等就是根據這個回調來實現最終注入依賴的屬性的。
@Nullable
default PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException {
return pvs;
}
}
SmartInstantiationAwareBeanPostProcessor
這個接口主要是spring框架內部來使用
public interface SmartInstantiationAwareBeanPostProcessor extends InstantiationAwareBeanPostProcessor {
//用來返回目標對象的類型(比如代理對象通過raw class獲取proxy type 用於類型匹配)
@Nullable
default Class<?> predictBeanType(Class<?> beanClass, String beanName) throws BeansException {
return null;
}
//這里提供一個拓展點用來解析獲取用來實例化的構造器(比如未通過bean定義構造器以及參數的情況下,會根據這個回調來確定構造器)
@Nullable
default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName)
throws BeansException {
return null;
}
//獲取要提前暴露的bean的引用,用來支持單例對象的循環引用(一般是bean自身,如果是代理對象則需要取用代理引用)
default Object getEarlyBeanReference(Object bean, String beanName) throws BeansException {
return bean;
}
}
MergedBeanDefinitionPostProcessor
用來將merged BeanDefinition暴露出來的回調
public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {
//在bean實例化完畢后調用 可以用來修改merged BeanDefinition的一些properties 或者用來給后續回調中緩存一些meta信息使用
//這個算是將merged BeanDefinition暴露出來的一個回調
void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);
}
DestructionAwareBeanPostProcessor
關於處理對象銷毀的前置回調
應用實例:
ApplicationListenerDetector,這個類是用來注冊ApplicationListener實例的,而如果銷毀一個對象,不接觸這里的引用
會導致無法進行回收,因此在銷毀對象時,會判斷如果是ApplicationListener要執行從監聽器列表中移除掉。
public interface DestructionAwareBeanPostProcessor extends BeanPostProcessor {
//這里實現銷毀對象的邏輯
void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException;
//判斷是否需要處理這個對象的銷毀
default boolean requiresDestruction(Object bean) {
return true;
}
}
關於BeanPostProcessor中各個回調調用的順序
1、InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation(beanClass, beanName)
該方法在創建對象之前會先掉用,如果有返回實例則直接使用不會去走下面創建對象的邏輯,並在之后執行
BeanPostProcessor.postProcessAfterInitialization(result, beanName)
2、SmartInstantiationAwareBeanPostProcessor.determineCandidateConstructors(beanClass, beanName)
如果需要的話,會在實例化對象之前執行
3、MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition(mbd, beanType, beanName)
在對象實例化完畢 初始化之前執行
4、InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)
在bean創建完畢初始化之前執行
5、InstantiationAwareBeanPostProcessor.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName)
在bean的property屬性注入完畢 向bean中設置屬性之前執行
6、BeanPostProcessor.postProcessBeforeInitialization(result, beanName)
在bean初始化(自定義init或者是實現了InitializingBean.afterPropertiesSet())之前執行
7、BeanPostProcessor.postProcessAfterInitialization(result, beanName)
在bean初始化(自定義init或者是實現了InitializingBean.afterPropertiesSet())之后執行
8、其中DestructionAwareBeanPostProcessor方法的postProcessBeforeDestruction(Object bean, String beanName)會在銷毀對象前執行
DestructionAwareBeanPostProcessor 中的requiresDestruction(Object bean)是用來判斷是否屬於當前processor處理的bean
SmartInstantiationAwareBeanPostProcessor中的predictBeanType(Class<?> beanClass, String beanName)是用來預判類型的
SmartInstantiationAwareBeanPostProcessor.getEarlyBeanReference(exposedObject, beanName)
這個方法僅僅是在這一步是作為一個ObjectFactory封裝起來放到singletonFactories中的,
僅在並發情況下 剛好在當前對象設置進去,而另一個bean創建需要getBean獲取時才會立即執行
因此這一步的順序是不一定的,有可能永遠不會執行(無並發循壞依賴對象創建的場景)
可能在3之后對象實例化完畢執行addSingleton(beanName, singletonObject);之前執行到
因此這三個方法沒有嚴格的順序意義
