java自定义package注解_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 packageannotation;2

3 importjava.lang.annotation.Documented;4 importjava.lang.annotation.ElementType;5 importjava.lang.annotation.Inherited;6 importjava.lang.annotation.Retention;7 importjava.lang.annotation.RetentionPolicy;8 importjava.lang.annotation.Target;9

10 /**

11 * Init.java12 *13 *@authorIT唐伯虎 2014年7月10日14 */

15 @Documented16 @Inherited17 @Target({ ElementType.FIELD, ElementType.METHOD })18 @Retention(RetentionPolicy.RUNTIME)19 public @interfaceInit20 {21 public String value() default "";22 }

2.在数据模型使用注解

1 packagemodel;2

3 importannotation.Init;4

5 /**

6 * User.java7 *8 *@authorIT唐伯虎 2014年7月10日9 */

10 public classUser11 {12 privateString name;13 privateString age;14

15 publicString getName()16 {17 returnname;18 }19

20 @Init(value = "liang")21 public voidsetName(String name)22 {23 this.name =name;24 }25

26 publicString getAge()27 {28 returnage;29 }30

31 @Init(value = "23")32 public voidsetAge(String age)33 {34 this.age =age;35 }36 }

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

1 packagefactory;2

3 importjava.lang.reflect.Method;4

5 importannotation.Init;6 importmodel.User;7

8 /**

9 * UserFactory.java10 *11 *@authorIT唐伯虎 2014年7月10日12 */

13 public classUserFactory14 {15 public staticUser create()16 {17 User user = newUser();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 returnuser;41 }42 }

4.运行的代码

1 packageapp;2

3 importjava.lang.reflect.InvocationTargetException;4

5 importfactory.UserFactory;6 importmodel.User;7

8 /**

9 * Test.java10 *11 *@authorIT唐伯虎 2014年7月10日12 */

13 public classTest14 {15 public static void main(String[] args) throwsIllegalAccessException,16 IllegalArgumentException, InvocationTargetException17 {18 User user =UserFactory.create();19

20 System.out.println(user.getName());21 System.out.println(user.getAge());22 }23 }

5.运行结果

1 liang2 23

三、通过注解进行校验

1.自定义注解

1 packageannotation;2

3 importjava.lang.annotation.Documented;4 importjava.lang.annotation.ElementType;5 importjava.lang.annotation.Inherited;6 importjava.lang.annotation.Retention;7 importjava.lang.annotation.RetentionPolicy;8 importjava.lang.annotation.Target;9

10 /**

11 * Validate.java12 *13 *@authorIT唐伯虎 2014年7月11日14 */

15 @Documented16 @Inherited17 @Target({ ElementType.FIELD, ElementType.METHOD })18 @Retention(RetentionPolicy.RUNTIME)19 public @interfaceValidate20 {21 public int min() default 1;22

23 public int max() default 10;24

25 public boolean isNotNull() default true;26 }

2.在数据模型使用注解

1 packagemodel;2

3 importannotation.Validate;4

5 /**

6 * User.java7 *8 *@authorIT唐伯虎 2014年7月11日9 */

10 public classUser11 {12 @Validate(min = 2, max = 5)13 privateString name;14

15 @Validate(isNotNull = false)16 privateString age;17

18 publicString getName()19 {20 returnname;21 }22

23 public voidsetName(String name)24 {25 this.name =name;26 }27

28 publicString getAge()29 {30 returnage;31 }32

33 public voidsetAge(String age)34 {35 this.age =age;36 }37 }

3.注解解析器

1 packagecheck;2

3 importjava.lang.reflect.Field;4

5 importannotation.Validate;6 importmodel.User;7

8 /**

9 * UserCheck.java10 *11 *@authorIT唐伯虎 2014年7月11日12 */

13 public classUserCheck14 {15 public static booleancheck(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()

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()

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 packageapp;2

3 importcheck.UserCheck;4 importmodel.User;5

6 /**

7 * Test.java8 *9 *@authorIT唐伯虎 2014年7月11日10 */

11 public classTest12 {13 public static voidmain(String[] args)14 {15 User user = newUser();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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值