1、环绕通知类的编写
public class LogAround implements org.aopalliance.intercept.MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
Object result = null;
//方法体1
try{
//方法体二
System.out.println("用环绕通知实现的前置通知[前置通知...]");
//methodInvocation.proceed()之前的代码就是前置通知
result = methodInvocation.proceed();//控制目标方法的执行,addStudent()
//methodInvocation.proceed()之后的代码就是后置通知
System.out.println("用环绕通知实现的后置通知[后置通知...]");
System.out.println("环绕通知的后置通知:目标对象:"+methodInvocation.getThis()+",调用的方法名"+methodInvocation.getMethod().getName()+",方法参数个数:"+methodInvocation.getArguments().length+",方法的返回值"+result);
//result就是目标方法addStudent()的返回值
}catch (Exception e){
//方法体三
//异常通知
System.out.println("用环绕通知实现的异常通知[异常通知...]");
}
return result;
}
}