Announcement Announcement Module
Collapse
No announcement yet.
Meta-Annotation @Component and Reflection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Meta-Annotation @Component and Reflection

    I am working on a project, where I am creating my own Meta-Annotation like

    @Targetů
    @Retentionů
    @Service
    @Transactional
    public @interface @MyTransactionalService

    And now I want to use Reflection to get the @Service on one of my classes. I know internally Spring is doing such stuff (I looked at the code, but couldn't find what I was looking for), and while my question here is not Spring specific, I thought since you guys have that expertise, you might impart some of that wisdom to me.

    So if I did myClass.getAnnotation(MyTransactionalService.class ) I would get the MyTransactionService annotation if myClass has that annotation. But when I try myClass.getAnnotation(Service.class) I get null.

    Thanks, and I promise what I am writing is something I am planning on contributing to a Spring Module.

    Mark

  • #2
    Originally posted by bytor99999 View Post
    I am working on a project, where I am creating my own Meta-Annotation like

    @Targetů
    @Retentionů
    @Service
    @Transactional
    public @interface @MyTransactionalService

    So if I did myClass.getAnnotation(MyTransactionalService.class ) I would get the MyTransactionService annotation if myClass has that annotation. But when I try myClass.getAnnotation(Service.class) I get null.

    Mark
    To access the @Service, @Transactional annotations -you would have to get the @MyTransanctionService annotation as you did and then get annotations defined on it.

    Code:
    @MyTransactionalService
    class ServiceImpl{}
    
    Annotation transactionAnnotation=ServiceImpl.class.getAnnotation(MyTransactionalService.class);
    
    for(Annotation annotation:transactionAnnotation.getannotationType().getAnnotations()){
    System.out.println(annotation.annotationType().getName());
    }

    Comment


    • #3
      Thanks Amit. That is what I was hoping to avoid. Meaning I created the meta-annotation so I wouldn't have to get all the different types, but based on what you are saying I would have to call getAnnotation(), then getAnnotation on that Annotation the first one returned. Which would, in my case double my calls to getAnnotation() method, instead of reducing it down to just one for all.

      I would have thought that in Spring's core code, the would just have to call getAnnotation(Component.class) and it would return the @Service, @Controller, @Repository annotation on the class.

      So I guess they then look for all the annotations calling getAnnotation(Service.class) and then getAnnotation(Controller.class) and then getAnnotation(Repository.class) instead of just one getAnnotation(Component.class)

      Bummer, maybe we need to add such a feature to Java 7 or 8 with Annotations.

      Mark

      Comment

      Working...
      X