spring应用手册-AOP(注解)-(16)-@Pointcut的表达式-@annotation

戴着假发的程序员出品 抖音ID:戴着假发的程序员 欢迎关注

@Pointcut的表达式-@annotation

spring应用手册(第三部分)


限制连接点的匹配,其中连接点的主题(在 Spring AOP 中执行的方法)具有给定的 annotation。

官方案例:

任何连接点(仅在 Spring AOP 中执行方法),其中执行方法具有@Transactional annotation:
@annotation(org.springframework.transaction.annotation.Transactional)

官方的案例已经说的很清楚了,就是@annotation是匹配拥有指定注解的方法的。这里要注意,@annotation只匹配实现类中的有注解的方法,不会匹配接口中的注解方法。

看案例:

我们准备接口:

/**
 * @author 戴着假发的程序员
 * 
 * @description
 */
public interface IBookService {
    //@DkAnnotation// 这里的注解是不会被匹配的
    public void saveBook(String title);
}

实现类:

/**
 * @author 戴着假发的程序员
 * 
 * @description
 */
@Component
public class BookService implements IBookService{
    @Override
    @DkAnnotation //这里的注解会被匹配
    public void saveBook(String title){
        System.out.println("保存图书:"+title);
    }
    public void saveBook(String title,int count){
        System.out.println("保存"+title+","+count+"次");
    }
}

修改Aspect类:

/**
 * @author 戴着假发的程序员
 * 
 * @description
 */
@Component //将当前bean交给spring管理
@Aspect //定义为一个AspectBean
public class DkAspect {
    //使用@annotation配置匹配所有还有指定注解的方法
    @Pointcut("@annotation(com.st.dk.demo7.annotations.DkAnnotation)")
    private void pointCut1(){}
    //定义一个前置通知
    @Before("pointCut1()")
    private static void befor(){
        System.out.println("---前置通知---");
    }
}

测试:

    @Test
    public void testAopPoint_annotation(){
        ApplicationContext ac =
                new AnnotationConfigApplicationContext(Appconfig.class);
        IBookService bean = ac.getBean(IBookService.class);
        bean.saveBook("程序员的修养");
    }

结果:
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
@annotation是一个切点表达式,它用于指定作用于方法上的注解。具体地说,当Spring扫描到带有该注解的方法时,切面通知就会被执行。在给定的引用内容中,@Pointcut("@annotation(com.test.aop.demo.MyAnnotation)")用于定义一个切点cutController(),它作用于使用@MyAnnotation注解的方法上。同样,@Pointcut("@within(com.test.aop.demo.MyAnnotation)")用于定义一个切点cutService(),它作用于被@MyAnnotation注解的类上。最后,@Pointcut("cutController() || cutService()")定义了一个切点cutAll(),它是cutController()和cutService()的逻辑或运算结果,即作用于使用@MyAnnotation注解的方法或被@MyAnnotation注解的类上。这些切点表达式使得我们可以在切面中选择性地应用通知,从而更灵活地控制切面的行为。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Spring Boot AOP @Pointcut拦截注解表达式与运算符](https://blog.csdn.net/u011974797/article/details/130085645)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【Spring AOP】@Aspect结合案例详解(一): @Pointcut使用@annotation + 五种通知Advice注解(已附源码)](https://blog.csdn.net/scm_2008/article/details/128593857)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

戴着假发的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值