Spring框架学习[AOP通知以及编程式AOP ](八)
2014-11-24 03:05:50
·
作者:
·
浏览: 10
造方法,初始化通知 public AfterReturningAdviceInterceptor(AfterReturningAdvice advice) { Assert.notNull(advice, "Advice must not be null"); this.advice = advice; } //后置通知拦截器回调方法 public Object invoke(MethodInvocation mi) throws Throwable { //调用ReflectiveMethodInvocation的proceed方法,调用通知链 Object retVal = mi.proceed(); //通知链调用最后才调用后置通知的返回之后回调方法 this.advice.afterReturning(retVal, mi.getMethod(), mi.getArguments(), mi.getThis()); return retVal; }}
(2).ThrowsAdviceInterceptor:
异常通知拦截器ThrowsAdviceInterceptor和前置通知拦截器MethodBeforeAdviceInterceptor、后置通知拦截器AfterReturningAdviceInterceptor类似,不同之处在于需要维护异常处理器,因此更加复杂,源码如下:
[java] view plaincopyprint public class ThrowsAdviceInterceptor implements MethodInterceptor, AfterAdvice { private static final String AFTER_THROWING = "afterThrowing"; private static final Log logger = LogFactory.getLog(ThrowsAdviceInterceptor.class); private final Object throwsAdvice; //key为类,value为method的异常处理map private final Map
exceptionHandlerMap = new HashMap
(); //异常通知拦截器构造方法 public ThrowsAdviceInterceptor(Object throwsAdvice) { Assert.notNull(throwsAdvice, "Advice must not be null"); //初始化异常通知 this.throwsAdvice = throwsAdvice; //获取异常通知的方法 Method[] methods = throwsAdvice.getClass().getMethods(); //遍历所有配置异常通知的方法 for (Method method : methods) { //如果方法名为"afterThrowing",且方法参数为1或4,且方法参数的父类为 //Throwable,说明异常通知中有异常处理器 if (method.getName().equals(AFTER_THROWING) && (method.getParameterTypes().length == 1 || method.getParameterTypes().length == 4) && Throwable.class.isAssignableFrom(method.getParameterTypes()[method.getParameterTypes().length - 1]) ) { //添加异常处理器 this.exceptionHandlerMap.put(method.getParameterTypes()[method.getParameterTypes().length - 1], method); if (logger.isDebugEnabled()) { logger.debug("Found exception handler method: " + method); } } } //没有配置异常处理器 if (this.exceptionHandlerMap.isEmpty()) { throw new IllegalArgumentException( "At least one handler method must be found in class [" + throwsAdvice.getClass() + "]"); } } //获取异常处理器数目 public int getHandlerMethodCount() { return this.exceptionHandlerMap.size(); } //获取异常处理器 private Method getExceptionHandler(Throwable exception) { //获取给定异常类 Class exceptionClass = exception.getClass(); if (logger.isTraceEnabled()) { logger.trace("Trying to find handler for exception of type [" + exceptionClass.getName() + "]"); } //从异常处理器map中获取给定异常类的处理器 Method handler = this.exceptionHandlerMap.get(exceptionClass); //如果异常处理器为null,且异常类不是Throwable while (handler == null && !exceptionClass.equals(Throwable.class)) { //获取异常类的基类 exceptionClass = exceptionClass.getSuperclass(); //获取基类的异常处理器 handler = this.exceptionHandlerMap.get(exceptionClass); } if (handler != null && logger.isDebugEnabled()) { logger.debug("Found handler for exception of type [" + exceptionClass.getName() + "]: " + handler); } return handler; } //异常通知的回调方法 public Object invoke(MethodInvocation mi) throws Throwable { //把目标对象方法调用放入try/catch中 try { return mi.proceed(); } catch (Throwable ex) { //在catch中触发异常通知的回调 Method handlerMethod = getExceptionHandler(ex); if (handlerMethod != null) { //使用异常处理器处理异常 invokeHandlerMethod(mi, ex, handlerMethod); } //将异常向上抛出 throw ex; } } //异常处理器处理异常 private void invokeHandlerMethod(MethodInvocation mi, Throwable ex, Method method) throws Throwable { Object[] handlerArgs; //如