Java自定义注解的使用场景和原理

自定义注解实例:

FruitName.java

复制代码
 1 import java.lang.annotation.Documented;
 2 import java.lang.annotation.Retention;
 3 import java.lang.annotation.Target;
 4 import static java.lang.annotation.ElementType.FIELD;
 5 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 6 
 7 /**
 8  * 水果名称注解
 9  */
10 @Target(FIELD)
11 @Retention(RUNTIME)
12 @Documented
13 public @interface FruitName {
14     String value() default "";
15 }
复制代码

  

FruitColor.java

复制代码
 1 import java.lang.annotation.Documented;
 2 import java.lang.annotation.Retention;
 3 import java.lang.annotation.Target;
 4 import static java.lang.annotation.ElementType.FIELD;
 5 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 6 
 7 /**
 8  * 水果颜色注解
 9  */
10 @Target(FIELD)
11 @Retention(RUNTIME)
12 @Documented
13 public @interface FruitColor {
14     /**
15      * 颜色枚举
16      */
17     public enum Color{ BLUE,RED,GREEN};
18     
19     /**
20      * 颜色属性
21      */
22     Color fruitColor() default Color.GREEN;
23 
24 }
复制代码

 

FruitProvider.java

复制代码
 1 import java.lang.annotation.Documented;
 2 import java.lang.annotation.Retention;
 3 import java.lang.annotation.Target;
 4 import static java.lang.annotation.ElementType.FIELD;
 5 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 6 
 7 
 8 /**
 9  * 水果供应者注解
10  */
11 @Target(FIELD)
12 @Retention(RUNTIME)
13 @Documented
14 public @interface FruitProvider {
15     /**
16      * 供应商编号
17      */
18     public int id() default -1;
19     
20     /**
21      * 供应商名称
22      */
23     public String name() default "";
24     
25     /**
26      * 供应商地址
27      */
28     public String address() default "";
29 }
复制代码

 

FruitInfoUtil.java

复制代码
 1 import java.lang.reflect.Field;
 2 
 3 /**
 4  * 注解处理器
 5  */
 6 public class FruitInfoUtil {
 7     public static void getFruitInfo(Class<?> clazz){
 8         
 9         String strFruitName=" 水果名称:";
10         String strFruitColor=" 水果颜色:";
11         String strFruitProvicer="供应商信息:";
12         
13         Field[] fields = clazz.getDeclaredFields();
14         
15         for(Field field :fields){
16             if(field.isAnnotationPresent(FruitName.class)){
17                 FruitName fruitName = (FruitName) field.getAnnotation(FruitName.class);
18                 strFruitName=strFruitName+fruitName.value();
19                 System.out.println(strFruitName);
20             }
21             else if(field.isAnnotationPresent(FruitColor.class)){
22                 FruitColor fruitColor= (FruitColor) field.getAnnotation(FruitColor.class);
23                 strFruitColor=strFruitColor+fruitColor.fruitColor().toString();
24                 System.out.println(strFruitColor);
25             }
26             else if(field.isAnnotationPresent(FruitProvider.class)){
27                 FruitProvider fruitProvider= (FruitProvider) field.getAnnotation(FruitProvider.class);
28                 strFruitProvicer=" 供应商编号:"+fruitProvider.id()+" 供应商名称:"+fruitProvider.name()+" 供应商地址:"+fruitProvider.address();
29                 System.out.println(strFruitProvicer);
30             }
31         }
32     }
33 }
复制代码

 

Apple.java

复制代码
 1 import test.FruitColor.Color;
 2 
 3 /**
 4  * 注解使用
 5  */
 6 public class Apple {
 7     
 8     @FruitName("Apple")
 9     private String appleName;
10     
11     @FruitColor(fruitColor=Color.RED)
12     private String appleColor;
13     
14     @FruitProvider(id=1,name="陕西红富士集团",address="陕西省西安市延安路89号红富士大厦")
15     private String appleProvider;
16     
17     public void setAppleColor(String appleColor) {
18         this.appleColor = appleColor;
19     }
20     public String getAppleColor() {
21         return appleColor;
22     }
23     
24     public void setAppleName(String appleName) {
25         this.appleName = appleName;
26     }
27     public String getAppleName() {
28         return appleName;
29     }
30     
31     public void setAppleProvider(String appleProvider) {
32         this.appleProvider = appleProvider;
33     }
34     public String getAppleProvider() {
35         return appleProvider;
36     }
37     
38     public void displayName(){
39         System.out.println("水果的名字是:苹果");
40     }
41 }
复制代码

 

FruitRun.java

复制代码
1 /**
2  * 输出结果
3  */
4 public class FruitRun {
5     public static void main(String[] args) {
6         FruitInfoUtil.getFruitInfo(Apple.class);
7     }
8 }
复制代码

 

运行结果是:

 水果名称:Apple
 水果颜色:RED
 供应商编号:1 供应商名称:陕西红富士集团 供应商地址:陕西省西安市延安路89号红富士大厦

阅读更多
个人分类: JAVA
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭