Results 1 to 5 of 5

Thread: Parametrizing annotations passed via stereotypes

  1. #1
    Join Date
    Jul 2005
    Posts
    18

    Default 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. #2
    Join Date
    Jul 2005
    Posts
    18

    Default Nothing? :(

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

  3. #3
    Join Date
    Jun 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Default 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.

  4. #4
    Join Date
    Jul 2005
    Posts
    18

    Default

    Quote 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 {
    ...
    }

  5. #5

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •