松江网站建设,姑苏美食标题网页设计素材,wordpress内存,本地最好的网站开发建设公司Aspect#xff08;切面#xff09;#xff1a;用于标识一个类是切面的注解。通常与其他通知注解一起使用#xff0c;定义切面类。
Pointcut#xff08;切点#xff09;#xff1a; 注解来定义切点#xff0c;它用于描述哪些连接点将会被通知所通知。
连接点#xff…Aspect切面用于标识一个类是切面的注解。通常与其他通知注解一起使用定义切面类。
Pointcut切点 注解来定义切点它用于描述哪些连接点将会被通知所通知。
连接点execution(* com.example.service.*.*(..))
通知类
Before前置通知在目标方法执行前执行。Around环绕通知在目标方法执行前后都执行并且可以控制是否执行目标方法。AfterReturning正常返回通知目标方法正常返回后执行。AfterThrowing异常返回通知在目标方法抛出异常后执行。After后置通知在目标方法执行后执行无论是否抛出异常都会执行。
执行顺序
1、使用前置通知
正常返回情况Before - 方法 - AfterReturning - After异常返回情况Before - 方法 - AfterThrowing - After
2、使用环绕通知
正常返回情况Around前- 方法 - Around后 - AfterReturning - After异常返回情况Around前- 方法 - Around后 - AfterThrowing - After
3、前置通知和环绕通知都使用
正常返回情况Before - Around前- 方法 - Around后- AfterReturning - After异常返回情况Before - Around前- 方法 - Around后 - AfterThrowing - After
注意在 Around 通知类型中通过调用 ProceedingJoinPoint.proceed() 才会触发目标方法的执行因此可以在方法执行前后加入额外的逻辑。
实现AOP案例
Aspect
Component
public class LoggingAspect {Before(execution(* com.example.MyService.*(..)))public void beforeMethodExecution(JoinPoint joinPoint) {String methodName joinPoint.getSignature().getName();System.out.println(Before executing method: methodName);}Around(execution(* com.example.MyService.*(..)))public Object aroundMethodExecution(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {String methodName proceedingJoinPoint.getSignature().getName();System.out.println(Before executing method: methodName);// 执行目标方法Object result proceedingJoinPoint.proceed();System.out.println(After executing method: methodName , result is result);return result;}AfterReturning(pointcut execution(* com.example.MyService.*(..)), returning result)public void afterMethodExecution(Object result) {System.out.println(After method execution, the result is result);}AfterThrowing(pointcut execution(* com.example.MyService.*(..)), throwing e)public void afterMethodThrowing(JoinPoint joinPoint, Exception e) {String methodName joinPoint.getSignature().getName();System.out.println(Method methodName threw exception: e.getMessage());}After(execution(* com.example.MyService.*(..)))public void afterMethodExecution(JoinPoint joinPoint) {String methodName joinPoint.getSignature().getName();System.out.println(After executing method: methodName);}
} ps以下是我整理的java面试资料感兴趣的可以看看。最后创作不易觉得写得不错的可以点点关注
链接https://www.yuque.com/u39298356/uu4hxh?# 《Java面试宝典》