AOP进阶-切入点表达式-@annotation

切入点表达式-@annotation

  • @annotation(注解全类名)切入点表达式,用于匹配标识有特定注解的方法
    • 首先创建一个注解类
      • package com.example.tlias.AOP;
        
        import java.lang.annotation.ElementType;
        import java.lang.annotation.Retention;
        import java.lang.annotation.RetentionPolicy;
        import java.lang.annotation.Target;
        
        @Retention(RetentionPolicy.RUNTIME) // todo 设置该注解何时生效
        @Target(ElementType.METHOD) // todo 设置该注解的作用范围
        public @interface MyLog {
        }
        
    •  然后在目标方法上进行注解标注
      •     @Override
            @MyLog
            public PageBean Page(Integer page, Integer pageSize, String name,
                                 Short gender,
                                 LocalDate begin, LocalDate end) {
                // 设置分页参数
                PageHelper.startPage(page, pageSize);
                // 执行正常查询操作
                List<Emp> empList = empMapper.list(name, gender, begin, end);
                Page<Emp> p = (Page<Emp>) empList;
                // 封装分页结果PageBean
                PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
                return pageBean;
            }
    • 最后在AOP类中设置切入点表达式 
      •     @Before("@annotation(com.example.tlias.AOP.MyLog)")
            public void before() {
                log.info("before....");
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: @annotation是AspectJ中的一个切点表达式,用于匹配被注解标记的方法。具体来说,它可以用于定义一个切点,该切点会匹配所有被指定注解标记的方法。这个注解可以用于AOP编程中,用于在方法执行前、执行后或抛出异常时执行一些额外的逻辑。例如,可以使用@annotation来定义一个切点,该切点会匹配所有被@MyAnnotation标记的方法,并在这些方法执行前或执行后执行一些额外的逻辑。 ### 回答2: AspectJ是一种切面编程语言,它使用pointcut和advice来定义和实现切面。pointcut是一组匹配连接点的规则,而advice则是在这些连接点上执行的行为。 在AspectJ中,可以使用@annotation来定义一个基于注解的pointcut。该pointcut将匹配所有被指定注解标注的方法。例如,以下AspectJ代码定义了一个基于@MyAnnotation注解的pointcut: ``` @Pointcut("@annotation(MyAnnotation)") public void myAnnotationPointcut() {} ``` 在这个例子中,@Pointcut注解用于定义一个切点,而@annotation(MyAnnotation)参数指定了一个基于MyAnnotation注解的pointcut。可以在匹配的连接点上执行advice。 例如,以下代码展示了如何使用上述pointcut来实现一个日志切面: ``` @Aspect public class LoggingAspect { @Before("myAnnotationPointcut()") public void logMethodCall(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MyAnnotation myAnnotation = signature.getMethod().getAnnotation(MyAnnotation.class); String message = myAnnotation.value(); System.out.println("Logging message: " + message); } } ``` 在此切面中,@Before注解用于指定advice将在匹配的连接点之前执行。在logMethodCall方法中,我们获取了匹配方法的注解信息,并且使用该信息进行日志记录。 总之,通过使用@annotation,我们可以定义基于注解的切点来匹配符合指定注解的方法,并执行针对该切点的advice。这在实现像日志记录和安全认证等切面时非常有用。 ### 回答3: pointcut是AOP中的一个概念,用于定义切入点,即在应用程序中哪些方法需要被织入到切面中。点切入点通常由切点表达式表示,在该表达式中,可以使用多种语法来匹配目标对象中的方法。点切入点也可以使用注解来表示。 @annotation是一个在点切入点中常用的语法,在其中,切入点将匹配带有特定注解的方法。注解可以用来标识目标方法的特殊行为或属性,例如,某个方法需要特殊的安全权限,或者某个方法需要记录其调用时间等。 具体来说,使用@annotation在pointcut中的语法如下: @Pointcut("@annotation(com.example.CustomAnnotation)") 其中,注解类名为com.example.CustomAnnotation。这意味着,只有带有这个注解的方法才会被匹配,然后被织入到切面中。 需要注意的是,在使用@annotation之前,需要先定义自定义注解。例如,可以定义一个名为CustomAnnotation的注解类: @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface CustomAnnotation { } 在这个例子中,注解类被标记为@Retention(RetentionPolicy.RUNTIME),这意味着注解将在运行时保留,并能够通过Java反射来访问它。注解类还用@Target(ElementType.METHOD)标记,这表示注解只能用于方法上。 综上所述,@annotation在pointcut中的使用方法如上所示。它可以用来匹配带有特定注解的方法,然后将这些方法织入到切面中。在使用之前,需要先定义自定义注解类,并标记其@Retention(RetentionPolicy.RUNTIME)和@Target(ElementType.METHOD)。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值