package com.bjsxt.aop; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class LogInterceptor { @Pointcut("execution(public * com.bjsxt.service..*.[color=red]add[/color](..))") public void myMethod(){}; @[color=red]Before[/color]("myMethod()") public void before() { System.out.println("method before"); } @[color=red]Around[/color]("myMethod()") public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable { System.out.println("method around start"); pjp.[color=red]proceed[/color](); System.out.println("method around end"); } @After("myMethod()") public void afterMethod() throws Throwable { System.out.println("method after"); } }
输出的结果为
method before method around start user saved! method after method around end
当调用 add 方法时,因为 Pointcut 的配置,会先执行Before 的声明下方法,然后再执行,Around 声明下的方法,在该方法里,执行到proceed 该字段时,回去调用 add的原方法,然后再到 after 方法里,再到proceed 字段下面的内容