当前位置: 代码网 > it编程>编程语言>Java > @annotation AOP编程的pointcut定义方式

@annotation AOP编程的pointcut定义方式

2025年09月15日 Java 我要评论
@annotation aop编程的pointcut定义通过 @pointcut("execution(* biz.baijing.service.impl.deptserviceimpl.*(..)

@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 切入点的方法。

只要增加注解,就能匹配复杂的「切入点表达式」。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com