图 1
咱们再回顾一下之前的事务源码剖析有提到,执行到 AbstractAutowireCapableBeanFactory.initializeBean()->applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName)->AbstractAutoProxyCreator.postProcessAfterInitialization()->AbstractAutoProxyCreator.wrapIfNecessary()->DefaultAopProxyFactory.createAopProxy() 链条创立代理;
public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {Class<?> targetClass = config.getTargetClass();
if (targetClass == null) {
throw new AopConfigException("TargetSource cannot determine target class:" +
"Either an interface or a target is required for proxy creation.");
}
// 如果是接口
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {return new JdkDynamicAopProxy(config);
}
// 如果不是接口
return new ObjenesisCglibAopProxy(config);
}
else {return new JdkDynamicAopProxy(config);
}
}
通过下面的形式判断 cglib 还是 jdk 动静代理;