Springboot:如何正确使用AOP

x33g5p2x  于2022-03-11 转载在 Spring  
字(5.8k)|赞(0)|评价(0)|浏览(395)

Springboot:如何正确使用AOP

一、AOP概念

  • 切面(Aspect)
  1. 一个关注点的模块化,这个关注点可能会横切多个对象。事务管理是J2EE应用中一个关于横切关注点的很好的例子。在Spring AOP中,切面可以使用基于模式或者基于@Aspect注解的方式来实现
  • 连接点(JoinPoint)
  1. 在程序执行过程中某个特定的点,比如某方法调用的时候或者处理异常的时候。在SpringAOP中,一个连接点总是表示一个方法的执行
  • 通知(Advice)
  1. 在切面的某个特定的连接点上执行的动作。其中包括了AroundBeforeAfter等不同类型的通知。许多AOP框架(包括Spring)都是以拦截器做通知模型,并维护一个以连接点为中心的拦截器链
  • 切入点(PointCut)
  1. 匹配连接点的断言。通知和一个切入点表达式关联,并在满足这个切入点的连接点上运行(例如,当执行某个特定名称的方法时),切入点表达式如何和连接点匹配是AOP的核心:Spring缺省使用AspectJ切入点语法
  • 引入(Intorduction)
  1. 用来给一个类型声明额外的方法或属性(也被称为连接类型声明)。Spring允许引入新的接口(以及一个对应的实现)到任何被代理的对象。例如,你可以使用引入来使一个bean实现接口,以便简化缓存机制
  • 目标对象(Target Object)
  1. 被一个或者多个切面所通知的对象。也被称做被通知对象。既然Spring AOP是通过运行时代理实现的,这个对象永远是一个被代理对象
  • AOP代理(Aop proxy)
  1. AOP框架创建的对象,用来实现切面契约(例如通知方法执行等等)。在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理
  • 植入(weaving)
  1. 把切面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时(例如使用AspectJ编译器),类加载时和运行时完成。Spring和其他纯Java AOP框架一样,在运行时完成织入

二、切点表达式

execution表达式

用于匹配方法执行的连接点,属于方法级别

语法:

execution(修饰符 返回值类型 方法名(参数)异常)

语法参数描述
修饰符可选,如public,protected,写在返回值前,任意修饰符填*号就可以
返回值类型必选,可以使用*来代表任意返回值
方法名必选,可以用*来代表任意方法
参数()代表是没有参数,(..)代表是匹配任意数量,任意类型的参数,当然也可以指定类型的参数进行匹配,如要接受一个String类型的参数,则(java.lang.String), 任意数量的String类型参数:(java.lang.String..)
异常可选,语法:throws 异常,异常是完整带包名,可以是多个,用逗号分隔

符号:

符号描述
*匹配任意字符
匹配多个包或者多个参数
+表示类及其子类

条件符:

符号描述
&&、and
||
!

案例

拦截com.gj.web包下的所有子包里的任意类的任意方法

  1. execution(* com.gj.web..*.*(..))

拦截com.gj.web.api.Test2Controller下的任意方法

  1. execution(* com.gj.web.api.Test2Controller.*(..))

拦截任何修饰符为public的方法

  1. execution(public * * (..))

拦截com.gj.web下的所有子包里的以ok开头的方法

  1. execution(* com.gj.web..*.ok*(..))

三、AOP通知

在切面类中需要定义切面方法用于响应响应的目标方法,切面方法即为通知方法,通知方法需要用注解标识,AspectJ支持5种类型的通知注解

注解描述
@Before前置通知, 在方法执行之前执行
@After后置通知, 在方法执行之后执行
@AfterReturn返回通知, 在方法返回结果之后执行
@AfterThrowing异常通知, 在方法抛出异常之后
@Around环绕通知,围绕方法的执行
  • @Before
  1. @Before("testCut()")
  2. public void cutProcess(JoinPoint joinPoint) {
  3. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  4. Method method = signature.getMethod();
  5. System.out.println("注解方式AOP开始拦截, 当前拦截的方法名: " + method.getName());
  6. }
  • @After
  1. @After("testCut()")
  2. public void after(JoinPoint joinPoint) {
  3. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  4. Method method = signature.getMethod();
  5. System.out.println("注解方式AOP执行的方法 :"+method.getName()+" 执行完了");
  6. }
  • @AfterReturn:其中value表示切点方法,returning表示返回的结果放到result这个变量中
  1. /**
  2. * returning属性指定连接点方法返回的结果放置在result变量中
  3. * @param joinPoint 连接点
  4. * @param result 返回结果
  5. */
  6. @AfterReturning(value = "testCut()",returning = "result")
  7. public void afterReturn(JoinPoint joinPoint, Object result) {
  8. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  9. Method method = signature.getMethod();
  10. System.out.println("注解方式AOP拦截的方法执行成功, 进入返回通知拦截, 方法名为: "+method.getName()+", 返回结果为: "+result.toString());
  11. }
  • @AfterThrowing:其中value表示切点方法,throwing表示异常放到e这个变量
  1. @AfterThrowing(value = "testCut()", throwing = "e")
  2. public void afterThrow(JoinPoint joinPoint, Exception e) {
  3. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  4. Method method = signature.getMethod();
  5. System.out.println("注解方式AOP进入方法异常拦截, 方法名为: " + method.getName() + ", 异常信息为: " + e.getMessage());
  6. }
  • @Around
  1. @Around("testCut()")
  2. public Object testCutAround(ProceedingJoinPoint joinPoint) throws Throwable {
  3. System.out.println("注解方式AOP拦截开始进入环绕通知.......");
  4. Object proceed = joinPoint.proceed();
  5. System.out.println("准备退出环绕......");
  6. return proceed;
  7. }

四、springboot中使用AOP

导出依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-web</artifactId>
  4. </dependency>

自定义注解

  1. package com.hl.springbootrunner.aop;
  2. import java.lang.annotation.ElementType;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. import java.lang.annotation.Target;
  6. @Retention(RetentionPolicy.RUNTIME)
  7. @Target(ElementType.METHOD)
  8. public @interface AOPAnnotations {
  9. }

创建切面类

  1. package com.hl.springbootrunner.aop;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.ProceedingJoinPoint;
  4. import org.aspectj.lang.annotation.*;
  5. import org.aspectj.lang.reflect.MethodSignature;
  6. import org.springframework.stereotype.Component;
  7. import java.lang.reflect.Method;
  8. @Aspect
  9. @Component
  10. public class TestAspect {
  11. /**
  12. * 这里的路径填自定义注解的全路径
  13. */
  14. @Pointcut("@annotation(com.hl.springbootrunner.aop.AOPAnnotations)")
  15. public void testCut() {
  16. }
  17. @Before("testCut()")
  18. public void cutProcess(JoinPoint joinPoint) {
  19. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  20. Method method = signature.getMethod();
  21. System.out.println("注解方式AOP开始拦截, 当前拦截的方法名: " + method.getName());
  22. }
  23. @After("testCut()")
  24. public void after(JoinPoint joinPoint) {
  25. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  26. Method method = signature.getMethod();
  27. System.out.println("注解方式AOP执行的方法 :" + method.getName() + " 执行完了");
  28. }
  29. @Around("testCut()")
  30. public Object testCutAround(ProceedingJoinPoint joinPoint) throws Throwable {
  31. System.out.println("注解方式AOP拦截开始进入环绕通知.......");
  32. Object proceed = joinPoint.proceed();
  33. System.out.println("准备退出环绕......");
  34. return proceed;
  35. }
  36. /**
  37. * returning属性指定连接点方法返回的结果放置在result变量中
  38. *
  39. * @param joinPoint 连接点
  40. * @param result 返回结果
  41. */
  42. @AfterReturning(value = "testCut()", returning = "result")
  43. public void afterReturn(JoinPoint joinPoint, Object result) {
  44. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  45. Method method = signature.getMethod();
  46. System.out.println("注解方式AOP拦截的方法执行成功, 进入返回通知拦截, 方法名为: " + method.getName() + ", 返回结果为: " + result.toString());
  47. }
  48. @AfterThrowing(value = "testCut()", throwing = "e")
  49. public void afterThrow(JoinPoint joinPoint, Exception e) {
  50. MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  51. Method method = signature.getMethod();
  52. System.out.println("注解方式AOP进入方法异常拦截, 方法名为: " + method.getName() + ", 异常信息为: " + e.getMessage());
  53. }
  54. }

自定义一个接口

  1. @RestController
  2. public class TestController {
  3. @GetMapping("/ok")
  4. @AOPAnnotations
  5. public String test2() {
  6. return "ok";
  7. }
  8. }

测试

相关文章