Inherit annotations from abstract class?

后端 未结 3 1063
Happy的楠姐
Happy的楠姐 2020-12-06 09:40

Can I somehow group a set of annotations on an abstract class, and every class that extends this class has automatically assigned these annotations?

At least the fol

3条回答
  •  孤城傲影
    2020-12-06 10:24

    The short answer is: with the annotations that you mentioned in your example, no.

    The long answer: there is a meta-annotation called java.lang.annotation.Inherited. If an annotation itself is annotated with this annotation, then when a class is annotated with it, its subclasses are also automatically annotated with it by implication.

    However, as you can see in the spring source code, the @Service and @Scope annotation are not themselves annotated with @Inherited, so the presence of @Service and @Scope on a class is not inherited by its subclasses.

    Maybe this is something that can be fixed in Spring.

提交回复
热议问题