views:

92

answers:

2

How can I combine annotations in Java?

EDIT I was asking if I two annotations a and b, can I combine to a single annotations c? If possible, how do I do that?

+3  A: 

Assuming you want to have multiple annotations on a single element, you can just list them in sequence.

The Wikipedia page on Java annotations has quite a few useful examples, e.g.

  @Entity                      // Declares this an entity bean
  @Table(name = "people")      // Maps the bean to SQL table "people"
  class Person implements Serializable {
     ...
  }
mikera
+2  A: 

You cannot combine the annotations by e.g. annotating the annotations, unless the annotation consumer will process the meta-annotation tree explicitly. For example, Spring supports such feature for @Transactional, @Component and some other annotations (you may wish to have a look at SpringTransactionAnnotationParser#parseTransactionAnnotation()). Nice to have this feature in Java core, but alas...

However you can declare the common parent class that has a set of annotations you need and extend it. But this is not always applicable.

dma_k