packagecom.gong.spring.aop.impl2;importjava.util.Arrays;importjava.util.List;importjavax.management.RuntimeErrorException;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.After;importorg.aspectj.lang.annotation.AfterReturning;importorg.aspectj.lang.annotation.AfterThrowing;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;public classLoggingAspect {public voidbeforeMethod(JoinPoint joinPoint) {
String methodName=joinPoint.getSignature().getName();
List args =Arrays.asList(joinPoint.getArgs());
System.out.println(methodName+" begin with "+args);
}public voidafterMethod(JoinPoint joinPoint) {//获取名字
String methodName =joinPoint.getSignature().getName();//获取参数
List args =Arrays.asList(joinPoint.getArgs());
System.out.println(methodName+" end with "+args);
}public voidafterReturning(JoinPoint joinPoint,Object result) {
String methodName=joinPoint.getSignature().getName();
List args =Arrays.asList(joinPoint.getArgs());
System.out.println("在afterReturning得到返回值:"+result);
System.out.println(methodName+" end with "+args);
}public voidafterThrowing(JoinPoint joinPoint,Exception ex) {
String methodName=joinPoint.getSignature().getName();
System.out.println(methodName+" occurs exception:"+ex);
}publicObject aroundMethod(ProceedingJoinPoint pjd) {
Object result= null;
String methodName=pjd.getSignature().getName();//执行目标方法
try{//前置通知
System.out.println(methodName+" begin with "+Arrays.asList(pjd.getArgs()));//执行目标方法
result =pjd.proceed();//后置通知
System.out.println("在aroundMethod中得到值:"+result);
}catch(Throwable e) {//TODO Auto-generated catch block//异常通知
System.out.println("the method occurs exception:" +e);throw newRuntimeException(e);
}//后置通知
System.out.println(methodName+" end with "+Arrays.asList(pjd.getArgs()));returnresult;
}
}