从抽象类继承注释? [英] 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?
至少以下内容不是work:
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;
}
推荐答案
答案是:no
除非注释类型上有@Inherited元注释,否则不会继承Java注释: https://docs.oracle.com/javase/7/docs/api/java/lang /annotation/Inherited.html 。
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的 @Component
注释上没有@Inherited ,所以你需要在每个组件类上放置注释。 @ Service,@ Controller和@Repository都没有。
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.
这篇关于从抽象类继承注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!