Java 接口注解?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/146391/
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-11 08:53:57  来源:igfitidea点击:

Annotations on Interfaces?

javaannotations

提问by Allain Lalonde

I can't figure out a use case for being able to annotate interfaces in Java.

我无法找出能够在 Java 中注释接口的用例。

Maybe someone could give me an example?

也许有人可以给我举个例子?

采纳答案by Alex Miller

I've used it in Spring to annotate interfaces where the annotation should apply to all subclasses. For example, say you have a Service interface and you might have multiple implementations of the interface but you want a security annotation to apply regardless of the annotation. In that case, it makes the most sense to annotate the interface.

我在 Spring 中使用它来注释接口,其中注释应该应用于所有子类。例如,假设您有一个 Service 接口,并且您可能有该接口的多个实现,但您希望无论注释如何都应用安全注释。在这种情况下,对界面进行注释是最有意义的。

回答by sblundy

More an example, but Localand Remoteannotations in EJB3. According to the java doc,

更多示例,但EJB3 中的本地远程注释。根据java文档,

When used on an interface, designates that interface as a local business interface.

在接口上使用时,将该接口指定为本地业务接口。

I guess the use case here is that the interface has a special function best denoted by an annotation.

我猜这里的用例是接口有一个特殊的功能,最好用注释来表示。

回答by Daren Thomas

You could use it for contract style programming - go one step further than just defining the interface (types and method names) and also define some semantics (contents of the types, preconditions, postconditions).

您可以将它用于契约风格的编程——比仅仅定义接口(类型和方法名称)更进一步,还定义一些语义(类型的内容、前置条件、后置条件)。

I'd have to check up on how annotations work in Java though, but this stuff could easily be done with Python annotations...

不过,我必须检查注释在 Java 中的工作方式,但是使用 Python 注释可以轻松完成这些工作...

回答by Daren Thomas

I use findbugsextensively. I find the use of annotationsto specify Nullity constraints very useful. Even if you dont actually use findbugs, it makes the intent of the code much clearer. Those annotations have their place on Interfaces as much as Classes. You could argue that it is kind of programming by contract ...

我广泛使用findbugs。我发现使用注释来指定 Nullity 约束非常有用。即使您实际上不使用 findbugs,它也会使代码的意图更加清晰。这些注解在接口和类上都有它们的位置。你可能会争辩说它是一种合同编程......

回答by Uri

I've seen a lot of research tools use method annotations to allow users to specify protocols, restrictions, etc. to facilitate automatic checking later.

我看到很多研究工具使用方法注释来允许用户指定协议、限制等,以方便以后的自动检查。

Since annotations don't dictate what you can do with them, there is no good reason not to allow users to annotate interfaces.

由于注释并没有规定您可以用它们做什么,因此没有充分的理由不允许用户对界面进行注释。

回答by skaffman

Even without examples, it should be clear to explain - interfaces describe behaviour, and so can annotations, so it's a logical match to put them together.

即使没有例子,解释起来也应该很清楚——接口描述行为,注释也可以,所以把它们放在一起是合乎逻辑的。

回答by user30684

When deploying applications under JBoss you can use annotations on a interface to export a service in the jmx-console.

在 JBoss 下部署应用程序时,您可以在接口上使用注解来导出 jmx-console 中的服务。

回答by Phyxx

I have used it when defining a REST interface. RESTeasy allows you to create REST client that uses an annotated interface (RESTInterfaceV1 in this example):

我在定义 REST 接口时使用过它。RESTeasy 允许您创建使用带注释的接口(本例中为 RESTInterfaceV1)的 REST 客户端:

final RESTInterfaceV1 client = ProxyFactory.create(RESTInterfaceV1.class, "http://localhost:8080/rest");

Although the annotations have to be duplicated on object that actually implements the REST interface, the interface itself can be distributed separately to those wanting to make a Java REST client.

尽管必须在实际实现 REST 接口的对象上复制注释,但接口本身可以单独分发给那些想要制作 Java REST 客户端的人。

回答by jontejj

I mark my interfaces with @Immutable to clearly indicate to developers of subclasses which Mutability contract a class implementing the interface should have. As gehel puts it, programming by contract.

我用@Immutable 标记我的接口,以向子类的开发人员清楚地指示实现接口的类应该具有哪些可变性契约。正如 gehel 所说,按合同编程。

回答by raspacorp

A use case that I am working with is javax/hibernate bean validation, we are using interfaces to help us on avoiding to define validations on every specific class.

我正在使用的一个用例是 javax/hibernate bean 验证,我们使用接口来帮助我们避免在每个特定类上定义验证。

public interface IUser {
    @NotNull Long getUserId();
    ...
}

public class WebUser implements IUser {
    private Long userId;

    @Override
    public Long getUserId(){
        return userId;
    }
    ...
}