AOP中获取方法上的注解/参数

通过aop方式(自定义注解)对方法进行前置/环绕/后置等处理时,可以通过以下方法,在加强处理方法中获取被加强方法的参数和注解等内容

 

自定义注解

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OperationLog {
    String description() default "";
}

aop类

@Aspect
@Component
public class WebOperationRecordHandle {
    
    @Pointcut("@annotation(com.test.aspect.OperationLog)")
    public void operationAspect() {
        
    }

    /*
    * 将被加强类的注解传入方法参数
    */
    @Before(value = "operationAspect() && @annotation(operationLog)", argNames = "operationLog")
    public void beforeHandel(OperationLog operationLog) {
        String description = operationLog.description();
        System.out.println(description);
    }

被加强的类

    @OperationLog(description = "测试方法")
    public Json test() {

    }

 

以下为获取被加强方法其他内容的写法

@Around(value = "args(param) && target(bean) && @annotation(logAnnotation)", argNames = "jp, param, bean, logAnnotation")
public void before(JoinPoint jp, String param, PersonService bean, LogAnnotation logAnnotation) {  
    ...
}

参考博客: http://loveshisong.cn/%E7%BC%96%E7%A8%8B%E6%8A%80%E6%9C%AF/2016-06-01-AOP%E4%B8%AD%E8%8E%B7%E5%8F%96%E6%96%B9%E6%B3%95%E4%B8%8A%E7%9A%84%E6%B3%A8%E8%A7%A3%E4%BF%A1%E6%81%AF.html

 

 

安利一门Python超级好课!
扫码下单输优惠码【csdnfxzs】再减5元,比官网还便宜!

https://marketing.csdn.net/u/xizolv/e/109

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值