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

SpringBoot中定位切点的两种常用方法

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

有时候,我们使用AOP来进行放的增强,编写切面类的时候,需要定位在哪个方法上试用该切面进行增强,本片文章主要讲解两种在SpringBoot中定位切点的方法,感兴趣的可以了解一下

有时候,我们使用AOP来进行放的增强,编写切面类的时候,需要定位在哪个方法上试用该切面进行增强,本片文章主要讲解两种在SpringBoot中定位切点的方法,一种是使用execution表达式的方法,一种则是利用自定义注解的方法。

接下来以一个简单的例子来讲解这两种方法的使用方式。

method();

execution 表达式

execution表达式的方式主要是在定义切点的时候,通过表达式的方式选取到所需要增强的方法。

execution表达式解读

 execution(?()?)
类型 解读 是否必须 示例
表示所选的修饰符类型 public/private/…
表示所选的返回值类型 void/int/…
表示所选的包或者方法 com.luke.service/com.luke.controller.*/…
() 表示所选方法的参数 *(..)/*(String name)/*(int size, ..)/…
表示所选方法的异常类型 throws Exception/…
 // 匹配指定包中的所有方法 execution(* com.luke.service.*(..)) // 匹配当前包中的所有public方法 execution(public * UserService.*(..)) // 匹配指定包中的所有public方法,并且返回值是int类型的方法 execution(public int com.luke.service.*(..)) // 匹配指定包中的所有public方法,并且第一个参数是String,返回值是int类型的方法 execution(public int com.luke.service.*(String name, ..)) 

自定义切面类:

 @Aspect @Component public class LogAspect { @Pointcut("execution(* com.luke.springdata.controller.*.*(..))") public void operationLog(){} /** * 这里只定义一个Around的增强做展示 */ @Around("operationLog()") public Object doAround(ProceedingJoinPoint joinPoint) { Object proceed = null; try { System.out.println("方法执行前"); proceed = joinPoint.proceed(); System.out.println("方法执行后"); } catch (Throwable throwable) { throwable.printStackTrace(); } return proceed; } }

此切点的execution表达式为com.luke.springdata.controller包下的所有方法。
使用**@Around**注解表明增强的方法,并且指定切点。

测试用Controller类

 @RestController @RequestMapping("/person") public class PersonController { @GetMapping("/test") public v<strong style="color:transparent">来源gaodai#ma#com搞@@代~&码*网</strong>oid test(){ System.out.println("方法执行了"); } } 

运行项目,调用该方法,查看结果。

方法执行前
方法执行了
方法执行后

自定义注解的方法

自定义注解的方式就是在需要增强的方法上面加上自定义的注解即可。

自定义注解类:

 @Documented @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Log{ } 

这里自定义了一个注解Log,该注解只能加在方法上。
自定义切面类:

 @Aspect @Component public class LogAspect { @Pointcut("@annotation(com.luke.springdata.annotation.Log)") public void operationLog(){} /** * 这里只定义一个Around的增强做展示 */ @Around("operationLog()") public Object doAround(ProceedingJoinPoint joinPoint) { Object proceed = null; try { System.out.println("方法执行前"); proceed = joinPoint.proceed(); System.out.println("方法执行后"); } catch (Throwable throwable) { throwable.printStackTrace(); } return proceed; } } 

这里编写的自定义个切面类,用**@Pointcut注解定义一个切面,并且这次采用@annotation(xxx)**的方式表明如果哪个方法上添加了xxx注解,则就使用该切面做增强。

同时在每个增强的方法上使用该切面,随后编写正常的方法增强逻辑即可。

测试用Controller类

 @RestController @RequestMapping("/person") public class PersonController { @Log @GetMapping("/test") public void test(){ System.out.println("方法执行了"); } } 

此时在需要使用切面的方法上加入**@Log**注解,调用该方法,查看效果。

方法执行前
方法执行了
方法执行后

总结

两种方式均能实现AOP的功能,在使用上,如果某个包下面的所有方法,都需要这个切面进行增强,那么使用execution表达式的方式更方便。但如果只有部分方法需要,并且分布在不同的类中,则注解的方式更灵活。

以上就是SpringBoot中定位切点的两种常用方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


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

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

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

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

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