背景
应用
基本内置注解
自定义注解
元注解
TestAnnotation.java
1 package Annotation; 2 3 import java.lang.annotation.ElementType; 4 import java.lang.annotation.Retention; 5 import java.lang.annotation.RetentionPolicy; 6 import java.lang.annotation.Target; 7 8 @Target(ElementType.TYPE) 9 @Retention(RetentionPolicy.RUNTIME) 10 public @interface TestAnnotation { 11 12 int id() default -1; 13 14 String msg() default "Hi"; 15 16 }
Test.java
1 import Annotation.TestAnnotation; 2 3 @TestAnnotation() 4 public class Test { 5 6 public static void main(String[] args) { 7 8 boolean hasAnnotation = Test.class.isAnnotationPresent(TestAnnotation.class); 9 10 if ( hasAnnotation ) { 11 TestAnnotation testAnnotation = Test.class.getAnnotation(TestAnnotation.class); 12 13 System.out.println("id:"+testAnnotation.id()); 14 System.out.println("msg:"+testAnnotation.msg()); 15 } 16 } 17 }
参考
秒懂,Java 注解 (Annotation)你可以这样学
https://blog.csdn.net/briblue/article/details/73824058
自定义注解
https://how2j.cn/k/annotation/annotation-customize/1056.html#nowhere
原文:https://www.cnblogs.com/cxc1357/p/12463557.html