Java自定义注解

转载:https://www.cnblogs.com/liangweiping/p/3837332.html

前言:本文包括三个部分:注解的基础、通过注解进行赋值(结合了工厂方法模式)、通过注解进行校验。

一、注解的基础

1.注解的定义:Java文件叫做Annotation,用@interface表示。

2.元注解:@interface上面按需要注解上一些东西,包括@Retention、@Target、@Document、@Inherited四种。

3.注解的保留策略:

  @Retention(RetentionPolicy.SOURCE)   // 注解仅存在于源码中,在class字节码文件中不包含

  @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得

  @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

4.注解的作用目标:

  @Target(ElementType.TYPE)                      // 接口、类、枚举、注解

  @Target(ElementType.FIELD)                     // 字段、枚举的常量

  @Target(ElementType.METHOD)                 // 方法

  @Target(ElementType.PARAMETER)            // 方法参数

  @Target(ElementType.CONSTRUCTOR)       // 构造函数

  @Target(ElementType.LOCAL_VARIABLE)   // 局部变量

  @Target(ElementType.ANNOTATION_TYPE) // 注解

  @Target(ElementType.PACKAGE)               // 包

5.注解包含在javadoc中:

  @Documented

6.注解可以被继承:

  @Inherited

7.注解解析器:用来解析自定义注解。

 

二、通过注解进行赋值(结合了工厂方法模式)

  1.自定义注解

 1 package annotation;
 2 
 3 import java.lang.annotation.Documented;
 4 import java.lang.annotation.ElementType;
 5 import java.lang.annotation.Inherited;
 6 import java.lang.annotation.Retention;
 7 import java.lang.annotation.RetentionPolicy;
 8 import java.lang.annotation.Target;
 9 
10 /**
11  * Init.java
12  * 
13  * @author IT唐伯虎 2014年7月10日
14  */
15 @Documented
16 @Inherited
17 @Target({ ElementType.FIELD, ElementType.METHOD })
18 @Retention(RetentionPolicy.RUNTIME)
19 public @interface Init
20 {
21     public String value() default "";
22 }

2.在数据模型使用注解

 1 package model;
 2 
 3 import annotation.Init;
 4 
 5 /**
 6  * User.java
 7  * 
 8  * @author IT唐伯虎 2014年7月10日
 9  */
10 public class User
11 {
12     private String name;
13     private String age;
14 
15     public String getName()
16     {
17         return name;
18     }
19 
20     @Init(value = "liang")
21     public void setName(String name)
22     {
23         this.name = name;
24     }
25 
26     public String getAge()
27     {
28         return age;
29     }
30 
31     @Init(value = "23")
32     public void setAge(String age)
33     {
34         this.age = age;
35     }
36 }

3.用“构造工厂”充当“注解解析器”

 1 package factory;
 2 
 3 import java.lang.reflect.Method;
 4 
 5 import annotation.Init;
 6 import model.User;
 7 
 8 /**
 9  * UserFactory.java
10  * 
11  * @author IT唐伯虎 2014年7月10日
12  */
13 public class UserFactory
14 {
15     public static User create()
16     {
17         User user = new User();
18 
19         // 获取User类中所有的方法(getDeclaredMethods也行)
20         Method[] methods = User.class.getMethods();
21 
22         try
23         {
24             for (Method method : methods)
25             {
26                 // 如果此方法有注解,就把注解里面的数据赋值到user对象
27                 if (method.isAnnotationPresent(Init.class))
28                 {
29                     Init init = method.getAnnotation(Init.class);
30                     method.invoke(user, init.value());
31                 }
32             }
33         }
34         catch (Exception e)
35         {
36             e.printStackTrace();
37             return null;
38         }
39 
40         return user;
41     }
42 }

4.运行的代码

 1 package app;
 2 
 3 import java.lang.reflect.InvocationTargetException;
 4 
 5 import factory.UserFactory;
 6 import model.User;
 7 
 8 /**
 9  * Test.java
10  * 
11  * @author IT唐伯虎 2014年7月10日
12  */
13 public class Test
14 {
15     public static void main(String[] args) throws IllegalAccessException,
16             IllegalArgumentException, InvocationTargetException
17     {
18         User user = UserFactory.create();
19 
20         System.out.println(user.getName());
21         System.out.println(user.getAge());
22     }
23 }

5.运行结果

1 liang
2 23

三、通过注解进行校验

  1.自定义注解

 1 package annotation;
 2 
 3 import java.lang.annotation.Documented;
 4 import java.lang.annotation.ElementType;
 5 import java.lang.annotation.Inherited;
 6 import java.lang.annotation.Retention;
 7 import java.lang.annotation.RetentionPolicy;
 8 import java.lang.annotation.Target;
 9 
10 /**
11  * Validate.java
12  * 
13  * @author IT唐伯虎 2014年7月11日
14  */
15 @Documented
16 @Inherited
17 @Target({ ElementType.FIELD, ElementType.METHOD })
18 @Retention(RetentionPolicy.RUNTIME)
19 public @interface Validate
20 {
21     public int min() default 1;
22 
23     public int max() default 10;
24 
25     public boolean isNotNull() default true;
26 }

2.在数据模型使用注解

 1 package model;
 2 
 3 import annotation.Validate;
 4 
 5 /**
 6  * User.java
 7  * 
 8  * @author IT唐伯虎 2014年7月11日
 9  */
10 public class User
11 {
12     @Validate(min = 2, max = 5)
13     private String name;
14 
15     @Validate(isNotNull = false)
16     private String age;
17 
18     public String getName()
19     {
20         return name;
21     }
22 
23     public void setName(String name)
24     {
25         this.name = name;
26     }
27 
28     public String getAge()
29     {
30         return age;
31     }
32 
33     public void setAge(String age)
34     {
35         this.age = age;
36     }
37 }

3.注解解析器

  1 package check;
  2 
  3 import java.lang.reflect.Field;
  4 
  5 import annotation.Validate;
  6 import model.User;
  7 
  8 /**
  9  * UserCheck.java
 10  * 
 11  * @author IT唐伯虎 2014年7月11日
 12  */
 13 public class UserCheck
 14 {
 15     public static boolean check(User user)
 16     {
 17         if (user == null)
 18         {
 19             System.out.println("!!校验对象为空!!");
 20             return false;
 21         }
 22 
 23         // 获取User类的所有属性(如果使用getFields,就无法获取到private的属性)
 24         Field[] fields = User.class.getDeclaredFields();
 25 
 26         for (Field field : fields)
 27         {
 28             // 如果属性有注解,就进行校验
 29             if (field.isAnnotationPresent(Validate.class))
 30             {
 31                 Validate validate = field.getAnnotation(Validate.class);
 32                 if (field.getName().equals("age"))
 33                 {
 34                     if (user.getAge() == null)
 35                     {
 36                         if (validate.isNotNull())
 37                         {
 38                             System.out.println("!!年龄可空校验不通过:不可为空!!");
 39                             return false;
 40                         }
 41                         else
 42                         {
 43                             System.out.println("年龄可空校验通过:可以为空");
 44                             continue;
 45                         }
 46                     }
 47                     else
 48                     {
 49                         System.out.println("年龄可空校验通过");
 50                     }
 51 
 52                     if (user.getAge().length() < validate.min())
 53                     {
 54                         System.out.println("!!年龄最小长度校验不通过!!");
 55                         return false;
 56                     }
 57                     else
 58                     {
 59                         System.out.println("年龄最小长度校验通过");
 60                     }
 61 
 62                     if (user.getAge().length() > validate.max())
 63                     {
 64                         System.out.println("!!年龄最大长度校验不通过!!");
 65                         return false;
 66                     }
 67                     else
 68                     {
 69                         System.out.println("年龄最大长度校验通过");
 70                     }
 71                 }
 72                 if (field.getName().equals("name"))
 73                 {
 74                     if (user.getName() == null)
 75                     {
 76                         if (validate.isNotNull())
 77                         {
 78                             System.out.println("!!名字可空校验不通过:不可为空!!");
 79                             return false;
 80                         }
 81                         else
 82                         {
 83                             System.out.println("名字可空校验通过:可以为空");
 84                             continue;
 85                         }
 86                     }
 87                     else
 88                     {
 89                         System.out.println("名字可空校验通过");
 90                     }
 91 
 92                     if (user.getName().length() < validate.min())
 93                     {
 94                         System.out.println("!!名字最小长度校验不通过!!");
 95                         return false;
 96                     }
 97                     else
 98                     {
 99                         System.out.println("名字最小长度校验通过");
100                     }
101 
102                     if (user.getName().length() > validate.max())
103                     {
104                         System.out.println("!!名字最大长度校验不通过!!");
105                         return false;
106                     }
107                     else
108                     {
109                         System.out.println("名字最大长度校验通过");
110                     }
111                 }
112             }
113         }
114 
115         return true;
116     }
117 }

4.运行的代码

 1 package app;
 2 
 3 import check.UserCheck;
 4 import model.User;
 5 
 6 /**
 7  * Test.java
 8  * 
 9  * @author IT唐伯虎 2014年7月11日
10  */
11 public class Test
12 {
13     public static void main(String[] args)
14     {
15         User user = new User();
16         
17         user.setName("liang");
18         user.setAge("1");
19         
20         System.out.println(UserCheck.check(user));
21     }
22 }

5.运行结果

1 名字可空校验通过
2 名字最小长度校验通过
3 名字最大长度校验通过
4 年龄可空校验通过
5 年龄最小长度校验通过
6 年龄最大长度校验通过
7 true

 

posted on 2019-03-18 14:07  江清澜静  阅读(256)  评论(0编辑  收藏  举报

导航