demo:
/1:*** 定义注解*/ @Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface FruitProvider { /**供应商编号*/ public int id() default -1; /*** 供应商名称*/ public String name() default ""; 13/04/2018 Page 108 of 283 /** * 供应商地址*/ public String address() default ""; } //2:注解使用 public class Apple { @FruitProvider(id = 1, name = "陕西红富士集团", address = "陕西省西安市延安路") private String appleProvider; public void setAppleProvider(String appleProvider) { this.appleProvider = appleProvider; } public String getAppleProvider() { return appleProvider; } } /3:*********** 注解处理器 ***************/ public class FruitInfoUtil { public static void getFruitInfo(Class<?> clazz) { String strFruitProvicer = "供应商信息:"; Field[] fields = clazz.getDeclaredFields();//通过反射获取处理注解 for (Field field : fields) { if (field.isAnnotationPresent(FruitProvider.class)) { FruitProvider fruitProvider = (FruitProvider) field.getAnnotation(FruitProvider.class); //注解信息的处理地方 strFruitProvicer = " 供应商编号:" + fruitProvider.id() + " 供应商名称:" + fruitProvider.name() + " 供应商地址:"+ fruitProvider.address(); System.out.println(strFruitProvicer); } } } } 13/04/2018 Page 109 of 283 public class FruitRun { public static void main(String[] args) { FruitInfoUtil.getFruitInfo(Apple.class); /***********输出结果***************/ // 供应商编号:1 供应商名称:陕西红富士集团 供应商地址:陕西省西安市延 } }
标签:String,供应商,public,处理器,FruitProvider,注解,class From: https://www.cnblogs.com/daitu66/p/18105114