简体   繁体   English

Java自定义注解的Processor代码什么时候调用? (运行时保留)

[英]When the Processor code of Java Custom Annotation is called? (runtime retention)

I know that Custom Java Annotations need a semi interface declaration with @interface and a processor code (in some text it's called the consumer code) which defines the actual business logic behind the annotation.我知道自定义 Java 注释需要一个带有@interface的半接口声明和一个处理器代码(在某些文本中称为消费者代码),它定义了注释背后的实际业务逻辑。 So far so clear, but my question is that, where/when exactly the processor code is called and by whom ?到目前为止很清楚,但我的问题是,处理器代码在哪里/何时被调用,由谁调用 I mean, does Java compiler generate the code which invokes all the annotation processors, for example, at class loading time?我的意思是,Java 编译器是否会生成调用所有注释处理器的代码,例如,在类加载时? or the programmer themselves decide when to call their processor codes for their custom annotations?或者程序员自己决定何时为他们的自定义注释调用他们的处理器代码? for example, @GetMapping("/users") on some method, tells the servlet API to map the requests with /users in their request path, should be delivered to this method.例如, @GetMapping("/users")在某些方法上,告诉 servlet API 将请求映射到请求路径中的 /users,应该传递给这个方法。 so it has some processor behind the scene which should be called to store this Mapping configuration somewhere , but when and who calls that processor?所以它在幕后有一些处理器,应该调用它来将这个映射配置存储在某处,但是何时以及谁调用该处理器? I guess my question makes sense only on the annotations with @Retention(RetentionPolicy.RUNTIME) .我想我的问题只对@Retention(RetentionPolicy.RUNTIME)的注释@Retention(RetentionPolicy.RUNTIME) Thanks谢谢

An annotation processor runs at compile time. 注释处理器在编译时运行。 When invoking javac , the user passes the -processor command-line option, and then javac invokes the processor.调用javac ,用户传递-processor命令行选项,然后javac调用处理器。 An annotation processor can issue errors, create files (including files of Java code), and even modify existing code.注释处理器可以发出错误、创建文件(包括 Java 代码文件),甚至修改现有代码。 In addition to the Oracle documentation linked above, you might want to see a tutorial, such as Baeldung's .除了上面链接的 Oracle 文档之外,您可能还想查看教程,例如Baeldung 的.

Some frameworks directly interpret annotations, so you don't need to write your own annotation processor.有些框架直接解释注解,所以不需要自己编写注解处理器。

To affect run-time behavior, your code can reflectively read annotations;为了影响运行时行为,您的代码可以反射性地读取注释; in this case no annotation processor is run at compile time.在这种情况下,在编译时没有运行注释处理器。

An Annotation as the name suggests, is additional information (or behavior).顾名思义, Annotation是附加信息(或行为)。 By default, this information is not processed or used to alter any behavior.默认情况下,此信息不会被处理或用于改变任何行为。 There has to be some entity to process the annotation.必须有一些实体来处理注释。

The retention policy tells the Java compiler about the scope of the annotation.保留策略告诉 Java 编译器注释的范围。 That is, whether the extra information is needed to be processed at runtime or compile-time.即,是否需要在运行时或编译时处理额外信息。

For compile-time processing, the annotation processors are hooked into the compilation process using the -processor flag.对于编译时处理, annotation processors使用-processor标志连接到编译过程中。 For run-time processing, reflection (such as this ) is used to access the additional information.对于运行时处理,反射(例如this )用于访问附加信息。

@GetMapping("/users") on some method, tells the servlet API to map the requests with /users in their request path, @GetMapping("/users") 在某些方法上,告诉 servlet API 将请求映射到请求路径中的 /users,

Just the presence of the annotation does not result in this behavior.仅注释的存在不会导致这种行为。 The spring framework (simplifying a lot) - spring框架(简化了很多)——

  • installs itself as the servlet endpoint for /* .将自身安装为/*的 servlet 端点。 See FrameworkServlet参见FrameworkServlet
  • enumerates the methods annotated with GetMapping using reflection使用反射枚举用GetMapping注释的方法
  • uses the annotated information to populate the mapping of URLs to methods.使用带注释的信息来填充 URL 到方法的映射。
  • while handling the requests, uses the mapping to route to the intended method.在处理请求时,使用映射路由到预期的方法。

One easy trick to understand the flow (yes, Java is too magicy sometimes) is to install breakpoints and look at the callstack.理解流程的一个简单技巧(是的,Java 有时太神奇了)是安装断点并查看调用堆栈。

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

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