Есть ли тег @class в Aspectj как тег @annotation? - PullRequest
0 голосов
/ 02 февраля 2020

В Spring AOP Aspectj есть тег @annotation, и мы можем легко аннотировать объект. Например:

Пользовательская аннотация:

public @interface Authorize
@Before("@annotation(authorize)")
public void adviseAnnotatedMethods(JoinPoint joinPoint, Authorize authorize) {
   System.out.println(authorize);
}

Интересно, есть ли тег @class, такой как @annotation?

@Before("@class(customClass)")
public void adviseAnnotatedMethods(JoinPoint joinPoint, CustomClass customClass {
   System.out.println(customClass);
}

Собственно, здесь, Моя цель - достичь объекта CustomClass. Я знаю, что я могу достичь этого из jointPoint.getArgs () в качестве проверки (объект instanceOf CustomClass). Но мне интересно, есть ли простой способ сделать это?


Обновление: полный код

Точный код:

У меня есть контроллер Метод:

@GetMapping("user/waiting/{accountId}/{userId}")
public ResponseEntity<?> getWaitingItems(@Custom CustomUser user) {
    ...
}

У меня есть аннотация, как показано ниже:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER, ElementType.TYPE})
public @interface Custom {}

CustomUser:

public class CustomUser {
    private Long accountId;
    private Long userId;
    private User user;
}

Методы Aspectj:

@Pointcut("execution(* *(.., @Custom (*), ..))")
void annotatedCustom() {}

@Before("annotatedCustom()")
public void adviseMethodsOfAnnotatedClass(JoinPoint joinPoint) {
    CustomUser cu = getArg(joinPoint);
    ...handle it.
}

private CustomClass getArg(JoinPoint joinPoint) {
    for (Object a : joinPoint.getArgs()) {
        if (a instanceof CustomClass) {
            return (CustomClass) a;
        }
    }
    return null;
}
...