可以通过指定Retention指示Java编译器对待注解的行为。默认级别为:CLASS。 以下是所有Retention级别:
| 策略 | 官方释义 | 中文释义 | 备注 |
|---|---|---|---|
| SOURCE | Annotations are to be discarded by the compiler. | 编译时丢弃 | 最低级别 |
| CLASS | Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time. This is the default behavior. | 编译时保留,运行时丢弃 | 默认级别 |
| RUNTIME | Annotations are to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively. | 运行时保留 | 最高级别 |