SpringFramework之DefaultAdvisorChainFactory

    Spring版本是5.0.4.release. 

    JdkDynamicAopProxy中使用到了拦截器链,如下List-1,advised是ProxyFactory,而方法getInterceptorsAndDynamicInterceptionAdvice则是在其父类AdvisedSupport中。

    List-1

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		...
		// Get the interception chain for this method.
		List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
		...
}

    如下List-2,advisorChainFactory是DefaultAdvisorChainFactory

    List-2

public List<Object> getInterceptorsAndDynamicInterceptionAdvice(Method method, @Nullable Class<?> targetClass) {
	MethodCacheKey cacheKey = new MethodCacheKey(method);
	List<Object> cached = this.methodCache.get(cacheKey);
	if (cached == null) {
		cached = this.advisorChainFactory.getInterceptorsAndDynamicInterceptionAdvice(
				this, method, targetClass);
		this.methodCache.put(cacheKey, cached);
	}
	return cached;
}

     getInterceptorsAndDynamicInterceptionAdvice的方法实现如下List-3所示,会将Advisor转换为Interceptor,List-3中的registry是DefaultAdvisorAdapterRegistry。

    List-3

public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializable {

	@Override
	public List<Object> getInterceptorsAndDynamicInterceptionAdvice(
			Advised config, Method method, @Nullable Class<?> targetClass) {
			...
			Interceptor[] interceptors = registry.getInterceptors(advisor);
			...		

		return interceptorList;
	}
	...

    如下List-4所示,构造方法中注册了Adapter,而后getInterceptors方法中会使用模板模式将advisor中的advice转换为MethodInterceptor。

    List-4

public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Serializable {

	private final List<AdvisorAdapter> adapters = new ArrayList<>(3);

	public DefaultAdvisorAdapterRegistry() {
		registerAdvisorAdapter(new MethodBeforeAdviceAdapter());
		registerAdvisorAdapter(new AfterReturningAdviceAdapter());
		registerAdvisorAdapter(new ThrowsAdviceAdapter());
	}

	@Override
	public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
		List<MethodInterceptor> interceptors = new ArrayList<>(3);
		Advice advice = advisor.getAdvice();
		if (advice instanceof MethodInterceptor) {
			interceptors.add((MethodInterceptor) advice);
		}
		for (AdvisorAdapter adapter : this.adapters) {
			if (adapter.supportsAdvice(advice)) {
				interceptors.add(adapter.getInterceptor(advisor));
			}
		}
		if (interceptors.isEmpty()) {
			throw new UnknownAdviceTypeException(advisor.getAdvice());
		}
		return interceptors.toArray(new MethodInterceptor[0]);
	}
	...

    来看个例子,如下List-5所示,MethodBeforeAdviceAdapter的supportsAdvice方法中判断是否是MethodBeforeAdvice;getInterceptor方法中将advice封装到MethodBeforeAdviceInterceptor中。

    List-5

class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {

	@Override
	public boolean supportsAdvice(Advice advice) {
		return (advice instanceof MethodBeforeAdvice);
	}

	@Override
	public MethodInterceptor getInterceptor(Advisor advisor) {
		MethodBeforeAdvice advice = (MethodBeforeAdvice) advisor.getAdvice();
		return new MethodBeforeAdviceInterceptor(advice);
	}
}

    MethodBeforeAdviceInterceptor的代码如下,比较简单,主要是invoke方法,会先调用MethodBeforeAdvice的before方法,之后才会执行MethodInvocation的invoke方法。

    List-6

public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeAdvice, Serializable {
	private final MethodBeforeAdvice advice;

	public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
		Assert.notNull(advice, "Advice must not be null");
		this.advice = advice;
	}

	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
		this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
		return mi.proceed();
	}
}

    整体来看DefaultAdvisorChainFactory会将我们的advice转换为MethodInerceptor,而不同的Advice实现,对应的MethodInterceptor实现也不一样,进而实现Before、After的拦截实现。

    从Spring容器中获取bean的时候,用于这个bean的拦截器就构造好,且与这个目标类封装在一起,之后当调用这个目标类的方法时,就会再次构造拦截器链,为什么会再次构造拦截器链是因为有些拦截器只作用与目标类的部分方法,不过这个基于方法层面的拦截器还是做了缓存的。

Reference

  1. https://github.com/spring-projects/spring-framework/tree/5.0.x

转载于:https://my.oschina.net/u/2518341/blog/3073829

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值