packagecom.example.demo.aop01;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;/*** 通知类,横切逻辑*/@Component
@Aspectpublic classAdvices {//切点
@Pointcut("execution(* com.example.demo.aop01.Math.a*(..))")public voidpointcut(){
}//前置通知
@Before("pointcut()")public voidbefore(JoinPoint jp){
System.out.println(jp.getSignature().getName());
System.out.println("----------前置通知----------");
}//最终通知
@After("pointcut()")public voidafter(JoinPoint jp){
System.out.println("----------最终通知----------");
}//环绕通知
@Around("execution(* com.example.demo.aop01.Math.s*(..))")public Object around(ProceedingJoinPoint pjp) throwsThrowable{
System.out.println(pjp.getSignature().getName());
System.out.println("----------环绕前置----------");
Object result=pjp.proceed();
System.out.println("----------环绕后置----------");returnresult;
}//返回结果通知
@AfterReturning(pointcut="execution(* com.example.demo.aop01.Math.m*(..))",returning="result")public voidafterReturning(JoinPoint jp,Object result){
System.out.println(jp.getSignature().getName());
System.out.println("结果是:"+result);
System.out.println("----------返回结果----------");
}//异常后通知
@AfterThrowing(pointcut="execution(* com.example.demo.aop01.Math.d*(..))",throwing="exp")public voidafterThrowing(JoinPoint jp,Exception exp){
System.out.println(jp.getSignature().getName());
System.out.println("异常消息:"+exp.getMessage());
System.out.println("----------异常通知----------");
}
}