javaguy
javaguy

Reputation: 4432

Combine annotations in Java

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?

Upvotes: 2

Views: 4619

Answers (2)

dma_k
dma_k

Reputation: 10639

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.

Upvotes: 6

mikera
mikera

Reputation: 106351

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

Upvotes: 2

Related Questions