简体   繁体   English

Java注释处理器:用法和应用程序

[英]Java Annotation Processors: Usage & Applications

I'm relatively new to Java EE development and although I understand what annotations are, and how to use them, I am having difficulty "seeing the forest through the trees" when it comes to understanding why anyone would need to write and process their own annotations. 我对Java EE开发相对较新,虽然我理解注释是什么,以及如何使用它们,但在理解为什么有人需要编写和处理自己的时候,我很难“看到森林穿过树林”。 注释。

Under what circumstances would you even need a custom annotation? 在什么情况下你甚至需要自定义注释?

I'm sure they are crucial somehow, I'm just don't see their usefulness for some reason. 我确信它们在某种程度上是至关重要的,我只是因为某种原因看不出它们的用处。

Thanks for any nudges in the right direction! 感谢任何正确方向的推动!

There are a lot of usages for custom annotations. 自定义注释有很多用法。 You might think that it's hard to find the reason why we would create one because many have been defined in other framework. 您可能认为很难找到我们创建一个的原因,因为许多已在其他框架中定义。 In order to basically answer your own question, you should ask "What happened if those frameworks didn't exist, would I need to create my custom use of annotations?" 为了基本回答你自己的问题,你应该问“如果那些框架不存在会发生什么,我是否需要创建自定义的注释使用?” The likely answer would be yes. 可能的答案是肯定的。 The following are some of few examples: 以下是一些例子:

  • If JAX-RS does not exists, you would probably think how to use annotations to signify the REST operation for the method such as the one described here 如果JAX-RS不存在,您可能会想到如何使用注释来表示方法的REST操作,例如此处描述的方法
  • If JUnit hasn't implemented the annotation to denote Test method like in this article , you might think to do the same thing 如果JUnit没有像本文中那样实现注释来表示Test方法,那么您可能会想到做同样的事情
  • If Spring hasn't implemented Validation, it's something that you might come up as well such as the one here 如果Spring没有实现验证,那么你可能会遇到这样的问题
  • And if Java itself hasn't come up with annotations for documentation, you probably would use your own custom annotations described here 如果Java本身没有提供文档注释,您可能会使用此处描述的自定义注释

To answer your question, everytime you want to enrich your class through additional metadata that hasn't been covered by other framework, you might be thinking of creating your own annotations. 要回答您的问题,每次您希望通过其他框架未涵盖的其他元数据来丰富您的课程时,您可能会考虑创建自己的注释。 While, a lot of smart people cover the common usages for annotations, that doesn't hinder you for coming up with your own usage in the future and thus the need for your own custom annotation and processing. 虽然,许多聪明的人都涵盖了注释的常见用法,但这并不妨碍您在未来提出自己的用法,因此需要您自己的自定义注释和处理。

There are some related discussions on StackOverflow, for instance here . 有关StackOverflow的一些相关讨论,例如这里

Simply put: whenever you implement your own framework which - in one way or the other - "glues" code together, enriches the code with some kind of "meta"-information that you process at build (or run-)time aso 简单地说:每当你实现自己的框架时 - 以某种方式 - 将代码“粘合”在一起,就可以在构建(或运行)时间处理的某种“元”信息中丰富代码。

Some well-known examples include: dependency injection, object-relational mapping, reflection, documentation etc. 一些众所周知的例子包括:依赖注入,对象关系映射,反射,文档等。

There's tons of reasons, but they're obviously application-specific. 有很多原因,但它们显然是特定于应用程序的。

For example, we annotate domain classes to be in-house-security-framework-aware. 例如,我们注释域类是内部安全框架感知的。 We process our own annotations for documentation purposes. 我们处理我们自己的注释以用于文档目的。 We have state-machine annotations that build up directed graphs for both configuration and documentation purposes. 我们有状态机注释,可以为配置和文档目的构建有向图。

Some are interrogated at run-time, some during documentation processing, some at startup, some have multiple uses. 有些是在运行时询问,有些是在文档处理期间,有些是在启动时,有些是有多种用途。

Framework annotations start off as being something custom, then when it's turned into a framework and released, they just seem "obvious" and "built-in", but they weren't necessarily always intended to be. 框架注释开始是自定义的东西,然后当它变成框架并发布时,它们看起来“显而易见”和“内置”,但它们并不一定总是有意。

IMHO they are one of the best things that you can use in Java and they save me ages. 恕我直言,他们是你可以在Java中使用的最好的东西之一,他们节省了我的年龄。 I'll give you an example: some days ago I was writing an image processing application. 我举个例子:几天前我正在编写一个图像处理应用程序。 All Filters extended a base class called Filter and many of them had properties like contrast, brightness, etc... Now each property will have a default value, a range with a min and a max, a name suitable for the gui. 所有过滤器都扩展了一个名为Filter的基类,其中许多都具有对比度,亮度等属性......现在每个属性都有一个默认值,一个带有min和max的范围,一个适合gui的名称。 Then there are properties on filters that I wanted the GUI to expose through controls, others that I didn't. 然后有过滤器的属性,我希望GUI通过控件公开,其他我没有。 So I created the @FilterPropertyAnnotation that you would use like this: 所以我创建了你将使用的@FilterPropertyAnnotation:

...
@FilterAnnotation(name = "Image contrast", default = 0.0D, min = -100D, max = 100D)
public double getContrast(){
...
}

Then, using reflection, every time the user choose a filter in the GUI it would scan that Filter subclass methods, see which ones have the annotation and display the appropriate controls only for the properties that I marked with the annotation, all of the with the correct default value and scale. 然后,使用反射,每次用户在GUI中选择一个过滤器时,它将扫描过滤子类的方法,查看哪些具有注释,并仅显示我用注释标记的属性的相应控件,所有这些都与正确的默认值和比例。 I can't think of a simpler or faster way of doing anything like this. 我想不出更简单或更快的方式来做这样的事情。

Annotations are for me the best thing in Java. 注释对我来说是Java中最好的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM