• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

SpringBoot AOP使用笔记

springboot 搞代码 4年前 (2022-01-05) 18次浏览 已收录 0个评论

今天小编就为大家分享一篇关于SpringBoot AOP使用笔记,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

1. 启用A来源gao.dai.ma.com搞@代*码网OP

a. 在类上添加@Aspect注解

b. 注入该类, 可以使用@Component进行注入到Spring容器中

2. 通过PointCut对象创建切入点

a. 在某个方法使用类似下面的方法进行注入

 @Pointcut("execution(* com.sguess.service.IAOPService.*(..))") private void pointcut() { }

i. 其中,execution表达式为
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)  
ii. 注意, pointcut()方法名是后面切入的时候需要使用的
iii. 方法内可以什么也不写, 写了也调不到
iv. 也可以创建多个PointCut,例如再创建一个

 @Pointcut("execution(* com.sguess.service.IAOPService.fun1(..))") private void pointcut2() { }

这个的方法名就位pointcut2, 方法名不一样.  

b. 创建After方法,Before方法

 @After(value = "pointcut()") public void doAfter() { System.out.println("Do AOP After function 01"); }

i. After方法是指, 在配置了的切入点被执行后, 执行该方法. 
ii. value中的pointcut() 是我们前面在创建@Pointcut中的方法名. 也就是说,是通过方法名和切入点进行匹配的. 
iii. 这个的方法名可以随便起. 
iv. Before方法同理

c. 带Return的After方法,

 @AfterReturning(returning = "str", pointcut = "pointcut()") public void doAfterReturning(String str) throws Exception { System.out.println("Return value is: " + str); }

i. AfterReturn是指在被切入的方法执行后, 获取其返回值, 再执行该方法. 注意关键, 这个可以进行操作返回值. 
ii. returning = “str”,是指, 假设切入方法的返回的值变量名为str
doAfterReturning(String str)方法的参数变量名必须和和returning保持一致, 这里也叫作str. 然后才能在方法体中使用.
iii. pointcut = “pointcut()”同样是指前面声明的pointcut方法名

3. 通过注解, 使用切入点

a. 监听方法参数

 @Before("execution(public int com.sguess.service.*(int, int))") public void beforMethod(JoinPoint point) { String methodName = point.getSignature().getName(); List args = Arrays.asList(point.getArgs()); System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args); } @After("execution(public int com.sguess.service.*(int, int))") public void afterMethod(JoinPoint point) { String methodName = point.getSignature().getName(); List args = Arrays.asList(point.getArgs()); System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args); }

4. 执行顺序:

a.Around的方法优先于Before/After执行,After优先于AfterReturn. 

i. 代码

 @Before("execution(public int com.sguess.service.*.*(int, int))") public void beforMethod(JoinPoint point) { System.out.println("Before function"); } @After("execution(public int com.sguess.service.*.*(int, int))") public void afterMethod(JoinPoint point) { System.out.println("After function"); } @AfterReturning("execution(public int com.sguess.service.*.*(int, int))") public void afterReturnMethod(JoinPoint point) { System.out.println("AfterReturn function"); } @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e") public void afterReturningThrowing(JoinPoint point, Exception e) { System.out.println("AfterReturnThrowing function"); } @Around("execution(public int com.sguess.service.*.*(int, int))") public Object aroundMethod(ProceedingJoinPoint pdj) { System.out.println("Start AroundFunction"); Object result = null; try { System.out.println("Around process start"); result = pdj.proceed(); System.out.println("Around process end"); } catch (Throwable e) { System.out.println("Around process exception"); } System.out.println("After Around process"); return result; } }

执行结果:

Start AroundFunction
Around process start
Before function
Around process end
After Around process
After function
AfterReturn function

5.小结:

 @AfterReturning(returning = "str", pointcut = "pointcut()") public void doAfterReturning(String str) throws Exception { System.out.println("Return value is: " + str); } @Before("execution(public int com.sguess.service.*.*(int, int))") public void beforMethod(JoinPoint point) { String methodName = point.getSignature().getName(); List args = Arrays.asList(point.getArgs()); System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args); } @After("execution(public int com.sguess.service.*.*(int, int))") public void afterMethod(JoinPoint point) { String methodName = point.getSignature().getName(); List args = Arrays.asList(point.getArgs()); System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args); } @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e") public void afterReturningThrowing(JoinPoint point, Exception e) { String methodName = point.getSignature().getName(); List args = Arrays.asList(point.getArgs()); System.out.println("AfterReturningThrowing FunctionName:" + methodName + ",ParameterName:" + args + ",Exception:" + e); } @Around("execution(public int com.sguess.service.*.*(int, int))") public Object aroundMethod(ProceedingJoinPoint pdj) { System.out.println("Start AroundFunction"); Object result = null; try { System.out.println("Around process start"); result = pdj.proceed(); System.out.println("Around process end"); } catch (Throwable e) { System.out.println("Around process exception"); } System.out.println("After Around process"); return result; }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对gaodaima搞代码网的支持。如果你想了解更多相关内容请查看下面相关链接

以上就是SpringBoot AOP使用笔记的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:SpringBoot AOP使用笔记

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址