经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
spring aop方法拦截器链
来源:cnblogs  作者:点胡  时间:2018/10/8 9:19:18  对本文有异议
  1. final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializable {}
  2. ]public Object invoke(Object proxy, Method method, Object[] args)
  3. >Object retVal;
  4. >List<Object> chain = this.advised.getInterceptorsAndDynamicInterceptionAdvice(method, targetClass);
  5. >if (chain.isEmpty()) {
  6. >+Object[] argsToUse = AopProxyUtils.adaptArgumentsIfNecessary(method, args);
  7. >+retVal = AopUtils.invokeJoinpointUsingReflection(target, method, argsToUse);
  8. >}
  9. >else {
  10. >+invocation = new ReflectiveMethodInvocation(proxy, target, method, args, targetClass, chain);//链式执行拦截(通知)方法
  11. >+retVal = invocation.proceed();
  12. >}
  13. [ReflectiveMethodInvocation.proceed()]
  14. public Object proceed() throws Throwable {
  15. // We start with an index of -1 and increment early.
  16. if (this.currentInterceptorIndex == this.interceptorsAndDynamicMethodMatchers.size() - 1) {
  17. return invokeJoinpoint();
  18. }
  19. Object interceptorOrInterceptionAdvice =
  20. this.interceptorsAndDynamicMethodMatchers.get(++this.currentInterceptorIndex);
  21. if (interceptorOrInterceptionAdvice instanceof InterceptorAndDynamicMethodMatcher) {
  22. // Evaluate dynamic method matcher here: static part will already have
  23. // been evaluated and found to match.
  24. InterceptorAndDynamicMethodMatcher dm =
  25. (InterceptorAndDynamicMethodMatcher) interceptorOrInterceptionAdvice;
  26. if (dm.methodMatcher.matches(this.method, this.targetClass, this.arguments)) {
    //调用拦截器的执行方法,拦截器执行拦截逻辑后继续调用目标方法的proceed()方法,参考下面的两个拦截器invoke()实现
  27. return dm.interceptor.invoke(this);
  28. }
  29. else {
  30. // Dynamic matching failed.
  31. // Skip this interceptor and invoke the next in the chain.
  32. return proceed();
  33. }
  34. }
  35. else {
  36. // It's an interceptor, so we just invoke it: The pointcut will have
  37. // been evaluated statically before this object was constructed.
  38. return ((MethodInterceptor) interceptorOrInterceptionAdvice).invoke(this);
  39. }
  40. }
  41. //before advice方法拦截器
  42. public class MethodBeforeAdviceInterceptor implements MethodInterceptor, BeforeAdvice, Serializable {
  43. private final MethodBeforeAdvice advice;
  44. /**
  45. * Create a new MethodBeforeAdviceInterceptor for the given advice.
  46. * @param advice the MethodBeforeAdvice to wrap
  47. */
  48. public MethodBeforeAdviceInterceptor(MethodBeforeAdvice advice) {
  49. Assert.notNull(advice, "Advice must not be null");
  50. this.advice = advice;
  51. }
  52. @Override
  53. public Object invoke(MethodInvocation mi) throws Throwable {
  54. this.advice.before(mi.getMethod(), mi.getArguments(), mi.getThis());
  55. return mi.proceed();
  56. }
  57. }
  58. //after advice方法拦截器
  59. public class AspectJAfterAdvice extends AbstractAspectJAdvice
  60. implements MethodInterceptor, AfterAdvice, Serializable {
  61. public AspectJAfterAdvice(
  62. Method aspectJBeforeAdviceMethod, AspectJExpressionPointcut pointcut, AspectInstanceFactory aif) {
  63. super(aspectJBeforeAdviceMethod, pointcut, aif);
  64. }
  65. @Override
  66. public Object invoke(MethodInvocation mi) throws Throwable {
  67. try {
  68. return mi.proceed();
  69. }
  70. finally {
  71. invokeAdviceMethod(getJoinPointMatch(), null, null);
  72. }
  73. }
  74. @Override
  75. public boolean isBeforeAdvice() {
  76. return false;
  77. }
  78. @Override
  79. public boolean isAfterAdvice() {
  80. return true;
  81. }
  82. }

 

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号