Java 从抽象类继承注解?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21409338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:33:22  来源:igfitidea点击:

Inherit annotations from abstract class?

javaspringdependency-injection

提问by membersound

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;
}

采纳答案by Philipp Sander

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.

除非注释类型具有 @Inherited 元注释,否则不会继承 Java 注释:https: //docs.oracle.com/javase/7/docs/api/java/lang/annotation/Inherited.html

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

Spring 的@Component注解上没有 @Inherited,因此您需要将注解放在每个组件类上。@Service、@Controller 和 @Repository 都不是。

回答by Kuba Spatny

I have this piece of code in my project and it works just fine, although it's not annotated as Service:

我的项目中有这段代码,它工作得很好,虽然它没有被注释为服务:

public abstract class AbstractDataAccessService {

    @Autowired
    protected GenericDao genericDao;


}

and

@Component
public class ActorService extends AbstractDataAccessService {

    // you can use genericDao here

}

So you don't need to put annotation on your abstract class but even if you do you will still have to put annotation on all subclass as @Componentor @Serviceannotations are not inherited.

因此,您不需要在抽象类上添加注释,但即使这样做,您仍然必须在所有子类上添加注释,@Component否则@Service注释不会被继承。

回答by Erwin Bolwidt

The short answer is: with the annotations that you mentioned in your example, no.

简短的回答是:使用您在示例中提到的注释,没有.

The long answer: there is a meta-annotation called java.lang.annotation.Inherited. If an annotation itself is annotated with this annotation, then when a class is annotated with it, its subclasses are also automatically annotated with it by implication.

答案很长:有一个名为java.lang.annotation.Inherited. 如果一个注解本身用这个注解进行注解,那么当一个类用它注解时,它的子类也自动用它进行注解。

However, as you can see in the spring source code, the @Serviceand @Scopeannotation are not themselves annotated with @Inherited, so the presence of @Serviceand @Scopeon a class is not inherited by its subclasses.

但是,正如您在 spring 源代码中看到的那样,@Service@Scope注释本身没有用 进行注释@Inherited,因此类上的@Service和的存在@Scope不会被其子类继承。

Maybe this is something that can be fixed in Spring.

也许这是可以在 Spring 中解决的问题。