@annotation AOP编程的pointcut定义方式
作者:RR1335
文章介绍通过注解简化AOP复杂结构定义,避免繁琐的逻辑组合,提升开发效率,并展示IDE如何辅助查看切入点匹配
@annotation AOP编程的pointcut定义
通过
@Pointcut("execution(* biz.baijing.service.impl.DeptServiceImpl.*(..))")
在定义复杂结构的时候通过 || 或 && 等关系定义,会很繁琐。
- 直接看代码
@Slf4j @Component @Aspect public class TryLoggingAspect { // 切入点表达式,引用 @Pointcut("@annotation(biz.baijing.aop.TryLogging)") public void poct() {} @Before("poct()") // * 任意方法 , .. 形参任意 public void before(){ log.info("Before ..."); } @After("poct()") public void after(){ log.info("After ..."); } }
通过
@Pointcut("@annotation(biz.baijing.aop.TryLogging)")
定义
- TryLogging 的代码
package biz.baijing.aop; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface TryLogging { // 示例 }
定义的 「注解」
定义到 service 方法上
m 又出现了 (
又 的前面出现在这里 AOP编程的基本概念与idea编辑器的配合体验
能看到作用到这个方法上的 pointcut 切入点的方法。
只要增加注解,就能匹配复杂的「切入点表达式」。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。