天津网站seo设计,湖州网站建设公司,莒县建设局官方网站,如何做网站网页一、通知类型
Around#xff1a;环绕通知#xff0c;此注解标注的通知方法在目标方法前、后都被执行
Before#xff1a;前置通知#xff0c;此注解标注的通知方法在目标方法前被执行
After#xff1a;后置通知#xff0c;此注解标注的通知方法在目标方法后被执行…一、通知类型
Around环绕通知此注解标注的通知方法在目标方法前、后都被执行
Before前置通知此注解标注的通知方法在目标方法前被执行
After后置通知此注解标注的通知方法在目标方法后被执行无论是否有异常都会执行
AfterReturning返回后通知此注解标注的通知方法在目标方法后被执行有异常不会执行
AfterThrowing异常后通知此注解标注的通知方法发生异常后执行 二、注意事项
1、Around环绕通知需要自己调用proceedingJoinPoint.proceed()来让原始方法执行其他通知不需要考虑目标方法执行
2、Around环绕通知方法的返回值必须指定Object来接收原始方法的返回值 三、抽取切入点表达式
PointCut将公共的切点表达式抽取出来需要用到时引用该切点表达式即可
public class MyAspect1 {Pointcut(execution(* com.itheima.service.impl.DeptServiceImpl.*(..)))public void pt(){}Before(pt())public void before(){log.info(before);
}
}
四、代码实现 Before(pt())public void before(){log.info(before);} Around(execution(* com.itheima.service.impl.DeptServiceImpl.*(..)))public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {log.info(around before);//调用目标对象的原始方法执行Object resultproceedingJoinPoint.proceed();log.info(around after);return result;}
After(execution(* com.itheima.service.impl.DeptServiceImpl.*(..)))public void after(){log.info(after);} AfterReturning(execution(* com.itheima.service.impl.DeptServiceImpl.*(..)))public void afterReturning(){log.info(afterReturning);} AfterThrowing(execution(* com.itheima.service.impl.DeptServiceImpl.*(..)))public void afterThrowing(){log.info(afterThrowing);}