spring-bean创建的生命周期与后置处理器的调用点
1.第一次调用BeanPostProcess , InstantiationAwareBeanPostProcessor中的postProcessBeforeInstantiation()方法.
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
// 第一次调用BeanPostProcess InstantiationAwareBeanPostProcessor这个处理器的postProcessBeforeInstantiation()方法.
// 如果一个对象,不想通过spring进行维护, 只是想放入spring容器当中, 就实现此接口, 反回一个你需要的对象出来
// 这边返回的对象不为null时, 这个beanName的的创建也聚完成了, spring的后置处理器不会对此对象进行操作.
// spring不推荐使用, 是spring提供内部添加对象时调用的.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
// Make sure bean class is actually resolved at this point.
// 判断是否有InstantiationAwareBeanPostProcessors的后置处理器.
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
Class> targetType = determineTargetType(beanName, mbd);
if (targetType != null) {
// 如果调用postProcessBeforeInstantiation返回了一个对象, 那么就直接执行后置处理器,此对象的实例化就完成.
bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
if (bean != null) {
bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
}
}
}
一般来说 , 实现此接口返回的对象, 都是不需要经过spring帮我们创建对象了 , 不需要spring进行管理了. spring不推荐我们使用, 而是交由spring内部自己调用的.
在spring-AOP有应用场景.
在我们开启AOP时. 添加 @EnableAspectJAutoProxy 注解. 基于spring的Import扩展 通过 ImportBeanDefinitionRegister, 帮我们注入了一个后置处理器.
AnnotationAwareAspectJAutoProxyCreator. 此类是 InstantiationAwareBeanPostProcessor的子类, 从写了 postProcessBeforeInstantiation()方法
@Override
public Object postProcessBeforeInstantiation(Class> beanClass, String beanName) throws BeansException {
Object cacheKey = getCacheKey(beanClass, beanName);
if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
// 再次判断beanClass中是否包含切面的注解 , 如果包含,保存到advisedBeans集合中,代表其实一个切面, 这样在bean实例化之后,
// 执行beanPostProcess的postProcessAfterInitialization方法时, 不会对切面进行增强.
if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return null;
}
}
// Create proxy here if we have a custom TargetSource.
// Suppresses unnecessary default instantiation of the target bean:
// The TargetSource will handle target instances in a custom fashion.
TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
if (StringUtils.hasLength(beanName)) {
this.targetSourcedBeans.add(beanName);
}
Object[] specificIntercept