1.目的
以前也稍微了解spring的源码,最近在研究研究,记录一下心得。
今天就针对spring中InstantiationAwareBeanPostProcessor的理解进行加深一下。
2.InstantiationAwareBeanPostProcessor是什么?
spring中基础对象都是bean! 那么InstantiationAwareBeanPostProcessor也是一个bean,只不过多了一层关系,实现InstantiationAwareBeanPostProcessor接口。
这个接口InstantiationAwareBeanPostProcessor是BeanPostProcessor扩展的子接口
3.InstantiationAwareBeanPostProcessor,BeanPostProcessor有什么区别?
BeanPostProcessor:有两个方法postProcessBeforeInitialization方法和postProcessAfterInitialization()。
在bean初始化的时候,也就是Spring激活bean的init-method方法的前后,会调用BeanPostProcessor的postProcessBeforeInitialization方法和postProcessAfterInitialization。
InstantiationAwareBeanPostProcessor:有三个方法
postProcessBeforeInstantiation(),postProcessAfterInstantiation(返回值boolean),postProcessPropertyValues()
1.在bean实例化过程(生成实例对象)前后调用。
涉及这两个方法 postProcessBeforeInstantiation(),postProcessAfterInstantiation(父接口中的方法)
2.当然也在初始化(setter注入,init方法)的过程中的setter注入这一步之前调用。涉及这两个方法 postProcessAfterInstantiation 返回值boolean,postProcessPropertyValues()
4.InstantiationAwareBeanPostProcessor如何被识别出来的?
因为InstantiationAwareBeanPostProcessor的父接口是beanPostProcessor,所以被设别的地方同beanPostProcessor地方一样
在AbstractApplicationContext方法中registerBeanPostProcessors()
代码行数:
版本:spring-core-3.0.0.RELEASE-sources.jar
方法中:AbstractApplicationContext.registerBeanPostProcessors() 401行
registerBeanPostProcessors(beanFactory);
protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
String[] postProcessorNames = beanFactory.getBeanNamesForType(BeanPostProcessor.class, true, false);
5.InstantiationAwareBeanPostProcessor中的方法是何时被调用?
5.1postProcessBeforeInstantiation(),postProcessAfterInstantiation(父接口中方法)何时被调用?
5.1.1在createBean方法里面有个resolveBeforeInstantiation方法
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, Object[] args) throws BeanCreationException {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
//省略....
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
return beanInstance;
}
上面代码里面看到,在执行doCreateBean之前有resolveBeforeInstantiation方法;doCreateBean是创建bean的方法;
resolveBeforeInstantiation是 判断执行InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation的接方法实现;
5.1.2
执行 postProcessBeforeInstantiation方法的时机
/**
* Apply before-instantiation post-processors, resolving whether there is a
* before-instantiation shortcut for the specified bean.
* @param beanName the name of the bean
* @param mbd the bean definition for the bean
* @return the shortcut-determined bean instance, or {@code null} if none
*/
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
Object bean = null;
//如果beforeInstantiationResolved还没有设置或者是false(说明还没有需要在实例化前执行的操作)
if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
// 判断是否有注册过InstantiationAwareBeanPostProcessor类型的bean
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
Class<?> targetType = determineTargetType(beanName, mbd);
if (targetType != null) {
//执行
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
}
mbd.beforeInstantiationResolved = (bean != null);
}
return bean;
}
protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
//只要有一个result不为null;后面的所有 后置处理器的方法就不执行了,直接返回(所以执行顺序很重要)
if (result != null) {
return result;
}
}
}
return null;
}
5.1.3执行postProcessAfterInstantiation(父接口中方法)的时机
@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
result = beanProcessor.postProcessAfterInitialization(result, beanName);
//如果返回null;后面的所有 后置处理器的方法就不执行,直接返回(所以执行顺序很重要)
if (result == null) {
return result;
}
}
return result;
}
5.2postProcessAfterInstantiation(返回值boolean)postProcessPropertyValues()何时被调用?
5.2.1执行postProcessAfterInstantiation(返回值boolean)的时机.
AbstractAutowireCapableBeanFactory.populateBean()
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
continueWithPropertyPopulation = false;
break;
}
}
}
}
5.2.2执行postProcessPropertyValues的时机
AbstractAutowireCapableBeanFactory.populateBean()
if (hasInstAwareBpps || needsDepCheck) {
PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw);
if (hasInstAwareBpps) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
if (pvs == null) {
return;
}
}
}
}
if (needsDepCheck) {
checkDependencies(beanName, mbd, filteredPds, pvs);
}
}
applyPropertyValues(beanName, mbd, bw, pvs);
6.参考目录
基本认知:
https://www.cnblogs.com/liuhongfeng/p/4581283.html
推荐
https://blog.csdn.net/u010634066/article/details/80321854