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

本文详细介绍了如何使用AOP(面向切面编程)和自定义注解来增强方法的处理,包括前置、环绕和后置操作。通过示例展示了如何在增强处理方法中获取被增强方法的参数和注解信息,适用于需要对方法调用进行拦截和额外处理的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过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.youkuaiyun.com/u/xizolv/e/109

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值