spring學習---spring AOP設計原理

目錄

  • 簡介
  • 生成代理方式類
  • 代理行爲
  • 流程圖
    • 切面主流程
    • 獲取通知鏈
    • 執行攔截或切面

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()

4. 流程圖

4.1 切面主流程

spring AOP切面

4.2 獲取通知鏈

spring APO獲取方法上通知鏈

4.3 執行攔截或切面

spring AOP應用通知


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章