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 63 64 65 66 67 68 69 70 71 72 73 74
| package com.course.advice;
import org.aopalliance.intercept.MethodInvocation; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;
@Component @Aspect public class MyDMLAdvice {
@Before("execution(* com.course.dao.impl.*.*(..))") public void openTransaction() { System.out.println("open transaction"); }
@After("execution(* com.course.dao.impl.*.*(..))") public void commitTransaction() { System.out.println("commit transaction"); }
@Around("execution(* com.course.dao.impl.*.*(..))") public void dmlTransaction(ProceedingJoinPoint pj){ try { openTransaction(); pj.proceed(); commitTransaction(); } catch (Throwable e) { e.printStackTrace(); } }
@AfterThrowing(value = "execution(* com.course.dao.impl.*.*(..))", throwing = "throwable") public void afterThrowing(Throwable throwable) throws Exception { System.out.println("产生异常:" + throwable.getMessage()); }
@Around("execution(* com.course.experiment.TimeConsumption.circulation(..))") public void computeTimeConsumption(ProceedingJoinPoint pj){ try { long startTime = System.currentTimeMillis(); pj.proceed(); long endTime = System.currentTimeMillis(); System.out.println("time consumption = " + (endTime - startTime) + " ms"); } catch (Throwable e) { e.printStackTrace(); } } }
|