Andrey Minogin
Andrey Minogin

Reputation: 4617

May @Repository annotation be inherited?

May @Repository annotation be inherited?

May I create a

@Repository
public abstract class BaseRepository {
}

and then extend it without specifying @Repository annotation?

public class MyRepository extends BaseRepository {
}

How do I know if an annotation can be inherited?

Upvotes: 4

Views: 2635

Answers (3)

esaj
esaj

Reputation: 16035

Annotations aren't inherited by subclasses by default, unless annotated themselves with @Inherited (see James Kingsbery's comment below), and even then only when they are used at class-level. @Repository-annotation itself is not annotated (I checked from the code) with @Inherited, so you'll have to annotate the subclass separately.

Upvotes: 2

James Kingsbery
James Kingsbery

Reputation: 7486

See http://www.docjar.com/html/api/org/springframework/stereotype/Repository.java.html

There is no @Inherited annotation, so it is not inherited.

Upvotes: 6

Bozho
Bozho

Reputation: 597116

No. The documentation about stereotypes says nothing about it.

Also, imagine the case when you specify a name @Repository("foo") - then each subclass will be named foo, which will lead to an exception (duplicate names)

Upvotes: 6

Related Questions