目錄
- 簡介
- 生成代理方式類
- 代理行爲
- 流程圖
- 切面主流程
- 獲取通知鏈
- 執行攔截或切面
1.簡介
Spring 提供了兩種方式來生成代理對象: JDKProxy和Cglib,具體使用哪種方式生成由AopProxyFactory 根據AdvisedSupport對象的配置來決定。默認的策略是如果目標類是接口,則使用JDK 動態代理技術,否則使用Cglib 來生成代理
2. 生成代理方式類
public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
@Override
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.");
}
//如果是接口,就用JdkProxy
if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
return new JdkDynamicAopProxy(config);
}
//非接口,就用cglib方式
return new ObjenesisCglibAopProxy(config);
}
else {
return new JdkDynamicAopProxy(config);
}
}
。。。。
}
3. 代理行爲
JdkDynamicAopProxy 實現了InvocationHandler接口,代理主要就是看invoke()