从抽象类继承注解? [英] 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;
}
推荐答案
答案是:没有
Java 注释不会被继承,除非注释类型上有 @Inherited 元注释: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屋!