When are Java annotations executed?

后端 未结 7 1996
执念已碎
执念已碎 2021-02-06 04:12

I am just looking to write some annotation which can execute at runtime, before or immediately after a service method is invoked.

I don\'t know if they are executed at r

7条回答
  •  暖寄归人
    2021-02-06 04:56

    It depends on retention policy attached with that annotation.

    A retention policy determines at what point annotation should be discarded. Java defined 3 types of retention policies through java.lang.annotation.RetentionPolicy enumeration. It has SOURCE, CLASS and RUNTIME.

    • Annotation with retention policy SOURCE will be retained only with source code, and discarded during compile time.

    • Annotation with retention policy CLASS will be retained till
      compiling the code, and discarded during runtime.

    • Annotation with retention policy RUNTIME will be available to the JVM through runtime.

    The retention policy will be specified by using java built-in annotation @Retention, and we have to pass the retention policy type. The default retention policy type is CLASS.

提交回复
热议问题