Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

java - Inherit annotations from abstract class?

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 following does not work:

@Service
@Scope(value = BeanDefinition.SCOPE_PROTOTYPE)
class AbstractService


class PersonService extends AbstractService {
    @Autowired //will not work due to missing qualifier annotation
    private PersonDao dao;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The answer is: no

Java annotations are not inherited unless the annotation type has the @Inherited meta-annotation on it: https://docs.oracle.com/javase/7/docs/api/java/lang/annotation/Inherited.html.

Spring's @Component annotation does not have @Inherited on it, so you will need to put the annotation on each component class. @Service, @Controller and @Repository neither.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...