Reputation: 719
I am building a custom annotation inside which there is a field of Class type. How do I set a value in it while using the annotation? Code is given below :
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SpringCache {
String putIt();
String getIt();
Class c();
}
This is my custom annotation. Now while I use it how do I give class to variable "c"?
@SpringCache(putIt="I'm put", getIt="I'm get", c=<??>)
public class TestSpringCache {
public static void main(String[] args) {
System.out.println();
}
}
As in this case I want c=TestSpringCache.class.
Upvotes: 2
Views: 430
Reputation: 719
Actually I didn't find the solution for this. As said by many it does not seem to be possible. Though I have found a work around this.
I removed the class type and kept only String and Boolean type variable there, and to get the annotated classes I took the path from the user to the package containing classes with the defined annotation. And then for each class in the package I checked for the annotation. If annotation was present I did what I wanted to, else did nothing.
Thanks to everyone for giving your time to this.
Upvotes: 1
Reputation: 86697
You have to optain all class instances from ApplicationContext
(you can inject it), ctx.getBean()
. There you can check if an annotation is present.
Upvotes: 1
Reputation: 3059
You just write the class you want:
@SpringCache(putIt="I'm put", getIt="I'm get", c=TestSpringCache.class)
public class TestSpringCache {
public static void main(String[] args) {
System.out.println();
}
}
You might have to add an import statement, if it is not in the same package.
Upvotes: 3