定义切点,切点表达式(execution(权限访问符 返回值类型 方法所属的类名包路径.方法名(形参类型) 异常类型))

  1. @Aspect
  2. @Component
  3. public class AfterThrowingAspect {
  4. @Pointcut("execution(* com.yh.test.*.*(..))")
  5. public void pointcut() {}
  6. .......
  7. }

@Before

前置增强,配合@Pointcut一起使用

  1. @Aspect
  2. @Component
  3. public class AfterThrowingAspect {
  4. //全路径execution(public String com.cnblogs.hellxz.test.Test.access(String,String))
  5. @Pointcut("execution(* com.yh.test.*.*(..))")
  6. public void pointcut() {}
  7. //前置增强
  8. @Before("pointcut()")
  9. public void before(JoinPoint jp){
  10. System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
  11. }
  12. }

@AfterReturning

后置增强,配合@Pointcut一起使用

  1. @Aspect
  2. @Component
  3. public class AfterThrowingAspect {
  4. @Pointcut("execution(* com.yh.test.*.*(..))")
  5. public void pointcut() {}
  6. //前置增强
  7. @Before("pointcut()")
  8. public void before(JoinPoint jp){
  9. System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
  10. }
  11. //后置增强
  12. @AfterReturning(value = "pointcut()",returning = "obj")
  13. public void afterReturning(JoinPoint jp,Object obj){
  14. System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
  15. }
  16. }

@Around

环绕增强,配合@Pointcut一起使用**

  1. @Aspect
  2. @Component
  3. public class AfterThrowingAspect {
  4. //全路径
  5. @Pointcut("execution(* com.yh.test.*.*(..))")
  6. public void pointcut() {}
  7. //前置增强
  8. @Before("pointcut()")
  9. public void before(JoinPoint jp){
  10. System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
  11. }
  12. //后置增强
  13. @AfterReturning(value = "pointcut()",returning = "obj")
  14. public void afterReturning(JoinPoint jp,Object obj){
  15. System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
  16. }
  17. //环绕增强
  18. @Around("pointcut()")
  19. public void around(ProceedingJoinPoint jp) throws Throwable {
  20. System.out.println("我是环绕增强中的前置增强!");
  21. Object proceed = jp.proceed();//植入目标方法
  22. System.out.println("我是环绕增强中的后置增强!");
  23. }
  24. }

@AfterThrowing

异常抛出增强,配合@Pointcut一起使用**

  1. @Aspect
  2. @Component
  3. public class AfterThrowingAspect {
  4. @Pointcut("execution(* com.yh.test.*.*(..))")
  5. public void pointcut() {}
  6. //前置增强
  7. @Before("pointcut()")
  8. public void before(JoinPoint jp){
  9. System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
  10. }
  11. //后置增强
  12. @AfterReturning(value = "pointcut()",returning = "obj")
  13. public void afterReturning(JoinPoint jp,Object obj){
  14. System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
  15. }
  16. //环绕增强
  17. @Around("pointcut()")
  18. public void around(ProceedingJoinPoint jp) throws Throwable {
  19. System.out.println("我是环绕增强中的前置增强!");
  20. Object proceed = jp.proceed();//植入目标方法
  21. System.out.println("我是环绕增强中的后置增强!");
  22. }
  23. //异常抛出增强
  24. @AfterThrowing(value = "pointcut()",throwing = "e")
  25. public void error(JoinPoint jp,Exception e){
  26. System.out.println("我是异常抛出增强"+",异常为:"+e.getMessage());
  27. }
  28. }

@After

最终增强(最后执行),配合@Pointcut一起使用**

  1. @Aspect
  2. @Component
  3. public class AfterThrowingAspect {
  4. //全路径execution(public String com.cnblogs.hellxz.test.Test.access(String,String))
  5. @Pointcut("execution(* com.cnblogs.hellxz.test.*.*(..))")
  6. public void pointcut() {}
  7. //前置增强
  8. @Before("pointcut()")
  9. public void before(JoinPoint jp){
  10. System.out.println("我是方法"+jp.getSignature().getName()+"的前置增强!");
  11. }
  12. //后置增强
  13. @AfterReturning(value = "pointcut()",returning = "obj")
  14. public void afterReturning(JoinPoint jp,Object obj){
  15. System.out.println("我是方法"+jp.getSignature().getName()+"的后置增强!"+",返回值为"+obj);
  16. }
  17. //环绕增强
  18. @Around("pointcut()")
  19. public void around(ProceedingJoinPoint jp) throws Throwable {
  20. System.out.println("我是环绕增强中的前置增强!");
  21. Object proceed = jp.proceed();//植入目标方法
  22. System.out.println("我是环绕增强中的后置增强!");
  23. }
  24. //异常抛出增强
  25. @AfterThrowing(value = "pointcut()",throwing = "e")
  26. public void error(JoinPoint jp,Exception e){
  27. System.out.println("我是异常抛出增强"+",异常为:"+e.getMessage());
  28. }
  29. //最终增强
  30. @After("pointcut()")
  31. public void after(JoinPoint jp){
  32. System.out.println("我是最终增强");
  33. }
  34. }