Maison  >  Questions et réponses  >  le corps du texte

java - Comment SpringAOP obtient-il les informations d'annotation sur la classe qui exécute la méthode?

La fonction que je souhaite réaliser est si elle est en class上注解了需要验证用户,那么里面的method就不需要逐个去写这个注解。
如果method写了注解,则以method.

Après vérification, j'ai constaté que la plupart des articles parlent de la manière d'obtenir les annotations sur method上的注解,而没有说到如何获得class.
S'il vous plaît, Dieu, donnez-moi un morceau de code.

Combiné à la réponse acceptée, le code complet est le suivant :

private AuthType getAuthType(ProceedingJoinPoint pj) {
        // 获取切入的 Method
        MethodSignature joinPointObject = (MethodSignature) pj.getSignature();
        Method method = joinPointObject.getMethod();

        boolean flag = method.isAnnotationPresent(AuthTarget.class);
        if (flag) {
            AuthTarget annotation = method.getAnnotation(AuthTarget.class);
            return annotation.value();
        } else {
            // 如果方法上没有注解,则搜索类上是否有注解
            AuthTarget classAnnotation = AnnotationUtils.findAnnotation(joinPointObject.getMethod().getDeclaringClass(), AuthTarget.class);
            if (classAnnotation != null) {
                return classAnnotation.value();
            } else {
                return null;
            }
        }
    }
天蓬老师天蓬老师2667 Il y a quelques jours929

répondre à tous(3)je répondrai

  • PHP中文网

    PHP中文网2017-06-30 09:56:55

    Utilisez les propres outils de Springorg.springframework.core.annotation.AnnotationUtils#findAnnotation(java.lang.Class<?>, java.lang.Class<A>)

    répondre
    0
  • 怪我咯

    怪我咯2017-06-30 09:56:55

    Vous pouvez lire cet article Annotations Java

    répondre
    0
  • typecho

    typecho2017-06-30 09:56:55

    aop coupé ici

    @Around("log() && @annotation(XXX.XXX.XXX.ControllerApiAnnotationLogin)")

    Annotations personnalisées

    /**
    *@auteur whmyit@163.com
    *@Time 2017-06-16

    • API de contrôle d'annotations personnalisées
      */

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.METHOD,ElementType.TYPE})
    public @interface ControllerApiAnnotationLogin {

      String name()  default "" ;   

    }

    répondre
    0
  • Annulerrépondre