2017-04-08 1 views
2

Supposons que je sois avec le haricot Initialisation code suivant:Est-il possible d'accéder à l'annotation de la méthode bean depuis l'intérieur du bean?

@Configuration 
MyConfig { 
    @Bean 
    @MyAnnotation // how to know this from bean constructor or method? 
    MyBean myBean() { 
     MyBean ans = new MyBean(); 
     /// setup ans 
     return ans; 
    } 
} 

Puis-je savoir à partir withing constructeur de haricots ou de withing méthodes de haricot de quelque chose @MyAnnotation annotation?

Pas de la méthode myBean(), ce qui est évident.

+0

Qu'entendez-vous par "quelque chose"? Votre annotation est-elle une annotation de rétention d'exécution? Est-ce que 'MyBean' a des informations sur' MyConfig' ou est-ce que vous vous attendez à ce qu'il soit au courant de l'annotation sans savoir comment il s'appelle? – RealSkeptic

+0

@RealSkeptic (1) "quelque chose" signifie l'existence de l'annotation et ses paramètres. (2) l'annotation est aussi "bonne" que nécessaire et c'est certainement runtime (3) non (4) Je suppose que Spring, lors de l'instanciation des beans de la config annotée, prend des informations à partir des annotations et est accessible pour les haricots – Dims

Répondre

1

Eh bien, il est sans doute pas la meilleure façon ou même correcte, mais ma première supposition est d'utiliser trace de la pile en cours et il semble fonctionner pour moi:

package yourpackage; 

import java.lang.annotation.Annotation; 
import java.lang.reflect.Method; 
import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 

public class AspectJRawTest { 

    public static void main(String[] args) { 
     System.out.println("custom annotation playground"); 

     ISomething something = new SomethingImpl(); 

     something.annotatedMethod(); 
     something.notAnnotatedMethod(); 
    } 

} 

interface ISomething { 
    void annotatedMethod(); 

    void notAnnotatedMethod(); 
} 

class SomethingImpl implements ISomething { 
    @MyCustomAnnotation 
    public void annotatedMethod() { 
     System.out.println("I am annotated and something must be printed by an advice above."); 
     CalledFromAnnotatedMethod ca = new CalledFromAnnotatedMethod(); 
    } 

    public void notAnnotatedMethod() { 
     System.out.println("I am not annotated and I will not get any special treatment."); 
     CalledFromAnnotatedMethod ca = new CalledFromAnnotatedMethod(); 
    } 
} 

/** 
* Imagine this is your bean which needs to know if any annotations affected it's construction 
*/ 
class CalledFromAnnotatedMethod { 
    CalledFromAnnotatedMethod() { 
     List<Annotation> ants = new ArrayList<Annotation>(); 
     for (StackTraceElement elt : Thread.currentThread().getStackTrace()) { 
      try { 
       Method m = Class.forName(elt.getClassName()).getMethod(elt.getMethodName()); 
       ants.addAll(Arrays.asList(m.getAnnotations())); 
      } catch (ClassNotFoundException ignored) { 
      } catch (NoSuchMethodException ignored) { 
      } 
     } 
     System.out.println(ants); 
    } 
} 

sortie montre clairement que je suis un accès à mon annotation dans le constructeur de l'objet lorsqu'il a été appelé à partir de la méthode annotée:

custom annotation playground 
I am annotated and something must be printed by an advice above. 
[@yourpackage.MyCustomAnnotation(isRun=true)] 
I am not annotated and I will not get any special treatment. 
[]