从抽象类继承注解? [英] Inherit annotations from abstract class?

查看:39
本文介绍了从抽象类继承注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能否以某种方式对抽象类上的一组注解进行分组,并且每个扩展该类的类都自动分配了这些注解?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆