返回到
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#postProcessAfterInitialization
if (!this.earlyProxyReferences.contains(cacheKey)) {
return wrapIfNecessary(bean, beanName, cacheKey);
}
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#wrapIfNecessary
// 获取类上的 advisor,即该 bean 的增强类,查看该方法
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;
}
org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator#createProxy
ProxyFactory proxyFactory = new ProxyFactory();
//获取该 beanName 的所有 Advisor
Advisor[] advisors = buildAdvisors(beanName, specificInterceptors);
// targetSource 目标对象,即service实现类
proxyFactory.setTargetSource(targetSource);
// 创建代理对象
return proxyFactory.getProxy(getProxyClassLoader());
org.springframework.aop.framework.ProxyFactory#getProxy(java.lang.ClassLoader)
return createAopProxy().getProxy(classLoader);
Cglib代理配置
<aop:config proxy-target-class="true">
//......
</aop:config>
Cglib代理类
org.springframework.aop.framework.CglibAopProxy#getProxy(java.lang.ClassLoader)
//目标类
Class<?> rootClass = this.advised.getTargetClass();
Class<?> proxySuperClass = rootClass;
//cglib
Enhancer enhancer = createEnhancer();
enhancer.setSuperclass(proxySuperClass);
// 作用:<aop:declare-parents implement-interface="com.dn.spring.aop.IntroductionIntf"
// 标签的配置接口
enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
Callback[] callbacks = getCallbacks(rootClass);
enhancer.setCallbackFilter(
new ProxyCallbackFilter(
this.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset));
// 创建代理类的实例
return createProxyClassAndInstance(enhancer, callbacks);
org.springframework.aop.framework.CglibAopProxy#getCallbacks
// 这个是最常用
Callback aopInterceptor = new DynamicAdvisedInterceptor(this.advised);
Callback[] mainCallbacks = new Callback[] {
aopInterceptor, // for normal advice
targetInterceptor, // invoke target without considering advice, if optimized
new SerializableNoOp(), // no override for methods mapped to this
targetDispatcher, this.advisedDispatcher,
new EqualsInterceptor(this.advised),
new HashCodeInterceptor(this.advised)
};
DynamicAdvisedInterceptor 实现了 org.springframework.cglib.proxy.MethodInterceptor 类
org.springframework.aop.framework.CglibAopProxy.DynamicAdvisedInterceptor#intercept
List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
if (chain.isEmpty() && Modifier.isPublic(method.getModifiers())) {
//链的拦截器为空时,则直接调用目标方法
Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
retVal = methodProxy.invoke(target, argsToUse);
}else {
// 调用拦截器方法
retVal = new CglibMethodInvocation(proxy, target, method, args, targetClass, chain, methodProxy).proceed();
}
CglibMethodInvocation 为继承了 org.springframework.aop.framework.ReflectiveMethodInvocation (这个类在jdk 动态代理时也用到了)
jdk 动态代理和 cglib 动态代理都会调用到 ReflectiveMethodInvocation#proceed 方法中,执行拦截器的调用