Announcement Announcement Module
Collapse
No announcement yet.
Parametrizing annotations passed via stereotypes Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Parametrizing annotations passed via stereotypes

    Hi,

    Is there a way to parametrize annotations passed to a bean via a stereotype? I've made a steretype like this:
    Code:
    @Target({ElementType.TYPE})
    @Retention(RetentionPolicy.RUNTIME)
    @Repository
    public @interface DictionaryDao {
    
    }
    @Repository annotation may receive a value determining name of a bean. Is there a way to pass that value through custom @DictionaryDao annotation? I'd like to add something like @DictionaryDao("nameOfADao") to my bean and get "nameOfADao" passed as value to @Repository annotation...

    I hope I've managed to make myself clear enough

    Regards,
    Wojtek

  • #2
    Nothing?

    Did I made that post that unreadable or does nobody know the answer?

    Comment


    • #3
      I am not sure what you are trying to accomplish

      If your goal is to simply have the string in the annotaion without having to explicitly assign it, all you have to do is make the value attribute an attribute in your annotation definition. Something like:
      Code:
      @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Repository
      public @interface DictionaryDao {
           String value() default "";
      }
      I am not really sure what your goal is beyond having a value attribute.

      Comment


      • #4
        Originally posted by solid View Post

        I am not really sure what your goal is beyond having a value attribute.
        My goal is to be able to pass custom annotation's attribute's to annotations of that custom annotation...
        Code:
        @Target({ElementType.TYPE})
        @Retention(RetentionPolicy.RUNTIME)
        @Repository([a way to pass defaultBeanName of DictionaryDao])
        @Transactional
        public @interface DictionaryDao {
             String value() defaultBeanName "";
        }
        So when I use my custom annotation like this:
        Code:
        @DictionaryDao(defaultBeanName="someNameForMyClass")
        public class MyClass {
        ...
        }
        would work in the same way as:
        Code:
        @Repository("someNameForMyClass")
        @Transactional
        public class MyClass {
        ...
        }

        Comment


        • #5
          I don't think this is going to do what you want it to do. I'm no annotations guru, but using meta-annotations on an annotation doesn't make the properties of the meta-annotation inheritable in the way you are expecting.

          Try this using the discreet @Repository and @Transactional annotations:

          Code:
          @Repository("someNameForMyClass")
          @Transactional
          public class MyClass {
          ...
          }
          
          MyClass.class.isAnnotationPresent(Repository.class); // returns "true"
          MyClass.class.isAnnotationPresent(Transactional.class); // returns "true"
          And then with your @DictionaryDao annotation:

          Code:
          @DictionaryDao(defaultBeanName="someNameForMyClass")
          public class MyClass {
          ...
          }
          
          MyClass.class.isAnnotationPresent(Repository.class); // returns "false"
          MyClass.class.isAnnotationPresent(Transactional.class); // returns "false"
          
          DictionaryDao.class.isAnnotationPresent(Repository.class); // returns "true"
          DictionaryDao.class.isAnnotationPresent(Transactional.class); // returns "true"
          In the second example, the @Repository and @Transactional annotations are applied to the @DictionaryDao type, not to the MyClass type, so Spring won't recognize MyClass the way you want it to. I don't know of a way to make the kind of aggregate annotation that you want.

          Comment

          Working...
          X