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) {  
    ...
}
posted @ 2021-08-17 17:33  zpk-aaron  阅读(121)  评论(0编辑  收藏  举报