AbstractAutoProxyCreator
SmartInstantiationAwareBeanPostProcessor
接口继承自InstantiationAwareBeanPostProcessor
接口。
因此AbstractAutoProxyCreator
一般是通过InstantiationAwareBeanPostProcessor
接口的postProcessBeforeInstantiation方法和postProcessAfterInitialization方法来返回代理对象;
- postProcessBeforeInstantiation阶段:前置处理器一般返回null,除非有TargetSource,就是对象已经创建了,则直接返回改bean
- postProcessAfterInitialization阶段:如果在postProcessBeforeInstantiation阶段返回null,则待对象创建并初始化完成,再通过postProcessAfterInitialization方法使用动态代理根据实例对象创建增强的动态代理对象。
public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport implements SmartInstantiationAwareBeanPostProcessor, BeanFactoryAware {
}
postProcessBeforeInstantiation
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
Object cacheKey = getCacheKey(beanClass, beanName);
if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
if (this.advisedBeans.containsKey(cacheKey)) {
return null;
}
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 targetSource = getCustomTargetSource(beanClass, beanName);
if (targetSource != null) {
if (StringUtils.hasLength(beanName)) {
this.targetSourcedBeans.add(beanName);
}
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
// 创建动态代理对象
Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
return null;
}
postProcessAfterInitialization
@Override
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) throws BeansException {
if (bean != null) {
Object cacheKey = getCacheKey(bean.getClass(), beanName);
if (!this.earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
getEarlyBeanReference
//获取提前暴露出来的bean的引用
public Object getEarlyBeanReference(Object bean, String beanName) {
//生成该bean对应的cacheKey
Object cacheKey = this.getCacheKey(bean.getClass(), beanName);
//将该bean放入到earlyProxyReferences中用于表示该Bean执行过AOP
this.earlyProxyReferences.put(cacheKey, bean);
//如果需要的话,对给定的bean进行封装,例如,该bean需要被代理
return this.wrapIfNecessary(bean, beanName, cacheKey);
}
postProcessAfterInitialization
//Create a proxy with the configured interceptors if the bean is identified as one to proxy by the subclass.
//如果 bean 被子类标识为代理,则使用配置的拦截器创建一个代理。
public Object postProcessAfterInitialization(@Nullable Object bean, String beanName) {
if (bean != null) {
Object cacheKey = this.getCacheKey(bean.getClass(), beanName);
//如果该bean未执行过AOP,则进行封装,如果执行过,则不再进行封装
if (this.earlyProxyReferences.remove(cacheKey) != bean) {
return this.wrapIfNecessary(bean, beanName, cacheKey);
}
}
return bean;
}
wrapIfNecessary
/**
* 如果需要的话,对给定的bean进行封装,例如,该bean需要被代理
* @param bean 原来的bean实例(the raw bean instance)
* @param beanName the name of the bean
* @param cacheKey the cache key for metadata access
* @return a proxy wrapping the bean, or the raw bean instance as-is
*/
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
if (isInfrastructureClass(bean.getClass()) || shouldSkip(bean.getClass(), beanName)) {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
// Create proxy if we have advice.
Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, null);
if (specificInterceptors != DO_NOT_PROXY) {
this.advisedBeans.put(cacheKey, Boolean.TRUE);
Object proxy = createProxy(
bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
}
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}