两个自定义注解
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface CanAdd {
RoleEnum[] role() default RoleEnum.ADMIN;
}
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface CanLook {
RoleEnum[] role() default RoleEnum.ADMIN;
}
在通知(advice)中获取注解的内容的两种方式
//第一种,使用这种方式需要将advice的参数与argNames、@annotation的参数一致
//表示拦截通知方法中入参的注解类型
@Before(value = "@annotation(canAdd)", argNames = "canAdd")
//@Order(1)
void addAdvice(CanAdd canAdd){
System.out.println(canAdd.role().length);
}
//第二种,通过JoinPoint
@Before(value = "lookPointCut()")
//@Order(4)
void lookAdvice(JoinPoint joinPoint){
CanLook look = ((MethodSignature)joinPoint.getSignature())
.getMethod().getAnnotation(CanLook.class);
System.out.println(look.role().length);
}
如果报 IncompleteAnnotationException 或 AnnotationTypeMismatchException ,再次编译项目。