When are Java annotations executed?

后端 未结 7 1951
执念已碎
执念已碎 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:46
    1. Annotations are only markers at the source code. They will be used by tools like IDE, compiler or annotation processors.

    2. You can define with @Retention if a annotation should be evaluated in the source, class or runtime.

    3. If you would like define your owned annotations and only you has the knowledge what the annotation should be do, so you should write an annotation processor too. (but this is not simple - maybe)

    0 讨论(0)
  • 2021-02-06 04:54

    Annotations don't have effect on your code, they're here to provide information to be evaluated by the compiler or other tools.

    Annotations have a number of uses, among them:

    Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.

    Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.

    Runtime processing — Some annotations are available to be examined at runtime.

    See this page.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-06 04:58

    They actually can be read in both environments, depending on the retention policy you set.

    0 讨论(0)
  • 2021-02-06 05:00

    Annotations are just markers. They don't execute and do anything.

    You can specify different retention policies:

    • SOURCE: annotation retained only in the source file and is discarded during compilation.
    • CLASS: annotation stored in the .class file during compilation, not available in the run time.
    • RUNTIME: annotation stored in the .class file and available in the run time.

    More here: http://www.java2s.com/Tutorial/Java/0020__Language/SpecifyingaRetentionPolicy.htm

    0 讨论(0)
  • 2021-02-06 05:03

    Actually, when you define an annotation, you must specify the parameter @Retention, which defines whether the annotation is available in the source code (SOURCE), in the class files (CLASS), or at run time (RUNTIME).

    @Retention(RetentionPolicy.RUNTIME)
    public @interface MyAnnotation {}
    
    0 讨论(0)
提交回复
热议问题