@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String FilePath();
}
其中:
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD})
TYPE: 当前注解可以用于类声明 、
FIELD:当前注解可以用于成员变量声明位置、
METHOD: 当前注解可以用于方法声明位置 。
@Retention(RetentionPolicy.RUNTIME)
当前注解参与代码运行
@MyAnnotation(FilePath = "F:\\test\\day25\\StudentSystemV1.4\\data\\students.json")
public class DataUtils {
//文件路径
private static String str;
//通过注解,得到文件路径
public static void main(String[] args) {
Class<DataUtils> cls = DataUtils.class;
MyAnnotation annotation = cls.getAnnotation(MyAnnotation.class);
str = annotation.FilePath();
System.out.println(str);
}
}
F:\test\day25\StudentSystemV1.4\data\students.json
Process finished with exit code 0