1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
@Aspect @Component public class ServiceAspect {
@Pointcut("execution(* site.xixing.service..*.*(..))") public void embed(){
}
@Before("embed()") public void before(JoinPoint joinPoint){ System.out.println("begin the joinPoint"+joinPoint); }
@After("embed()") public void after(JoinPoint joinPoint){ System.out.println("end the joinPoint"+joinPoint); }
@Around("embed()") public Object around(JoinPoint joinPoint){ long start=System.currentTimeMillis(); System.out.println("开始计时:"+start); Object returnValue=null;
try { ((ProceedingJoinPoint) joinPoint).proceed();
System.out.println("执行成功"); } catch (Throwable throwable) { throwable.printStackTrace(); }finally { long endTime=System.currentTimeMillis(); System.out.println("执行时间:"+(endTime-start)); } return returnValue; }
@AfterReturning(pointcut = "embed()" ,returning = "returnValue") public void afterReturning(JoinPoint joinPoint,Object returnValue){ System.out.println("afterReturning:"+joinPoint+"最后返回:"+returnValue); }
@AfterThrowing(pointcut = "embed()" ,throwing = "exception") public void afterThrowing(JoinPoint joinPoint,Exception exception){ System.out.println("抛出异常通知:"+joinPoint+exception.getMessage()); }
}
|