Java 自定义注解

我们在使用Spring框架的时候,会经常使用类似:@Autowired 这样的注解。我们也可以自己定义一些注解。Java的注解主要在包:java.lang.annotation中实现。

1. 元注解

什么是元注解?你可以这样理解,元注解是自定义注解的注解。元注解主要包含4个。他们主要在java.lang.annotation中可以找到。我们自己要创建注解的时候必须要用到这些元注解。所以必须彻底理解这四个元注解的含义。

1. @Documented

2. @Inherited

3. @Retention

4. @Target

例如:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  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.   

  12. /** 

  13.  * 定义一个用户名的自定义注解 

  14.  * @author zhuli 

  15.  * @date 2014-7-5 

  16.  */  

  17. @Documented  

  18. @Retention(RetentionPolicy.RUNTIME)  

  19. @Target({ ElementType.TYPE, ElementType.METHOD})  

  20. @Inherited  

  21. public @interface UserNameAnnotations {  

  22.   

  23.     public String value() default "";  

  24.   

  25. }  

1. @Documented

@Documented用于描述其它类型的annotation应该被作为被标注的程序成员的公共API,因此可以被例如javadoc此类的工具文档化。Documented是一个标记注解,没有成员。

2. @Inherited

@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。

3. @Target

@Target说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。

ElementType.CONSTRUCTOR  作用于构造器
ElementType.FIELD  作用于域/属性
ElementType.LOCAL_VARIABLE  用于描述局部变量
ElementType.METHOD  作用于方法
ElementType.PACKAGE   用于描述包
ElementType.PARAMETER   用于描述参数
ElementType.TYPE   用于描述类、接口(包括注解类型) 或enum声明,最常用

单个修饰对象的范围:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. @Target(ElementType.TYPE)  


多个:

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. @Target({ ElementType.TYPE, ElementType.METHOD})  

4. Retention

定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。使用这个meta-Annotation可以对 Annotation的“生命周期”限制。

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

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

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


2. 创建一个自定义注解 - 作用于类

1. 创建一个注解类

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  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.   

  12. /** 

  13.  * 定义一个用户名的自定义注解 

  14.  * @author zhuli 

  15.  * @date 2014-7-5 

  16.  */  

  17. @Documented //文档  

  18. @Retention(RetentionPolicy.RUNTIME) //在运行时可以获取  

  19. @Target({ ElementType.TYPE, ElementType.METHOD}) //作用到类,方法,接口上等  

  20. @Inherited //子类会继承  

  21. public @interface UserNameAnnotations {  

  22.       

  23.     public String value() default ""//使用的时候 @UserNameAnnotations(value="xxx")  

  24.   

  25. }  


2. 创建一个Test类

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  2.   

  3.   

  4. /** 

  5.  * 一个注解的测试类 

  6.  * @author zhuli 

  7.  * @date 2014-7-5 

  8.  */  

  9. //注入注解作用于类上面  

  10. //可以通过反射 获取类的信息之后 获取得到这个注解的值  

  11. @UserNameAnnotations(value = "initphp")   

  12. public class Test {  

  13.   

  14.     private String userName;  

  15.   

  16.     public String getUserName() {  

  17.         return userName;  

  18.     }  

  19.   

  20.     public void setUserName(String userName) {  

  21.         this.userName = userName;  

  22.     }  

  23.   

  24. }  



3. 测试类

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  2.   

  3.   

  4. public class mainTest {  

  5.   

  6.     public static void main(String[] args) {  

  7.         Class<Test> testClass = Test.class;  

  8.         //因为注解是作用于类上面的,所以可以通过isAnnotationPresent来判断是否是一个  

  9.         //有UserNameAnnotations注解的类  

  10.         if (testClass.isAnnotationPresent(UserNameAnnotations.class)) {  

  11.             System.out.println("this is a Annotations class");  

  12.             //通过getAnnotation可以获取注解对象  

  13.             UserNameAnnotations userNameAnnotations = (UserNameAnnotations) testClass.  

  14. getAnnotation(UserNameAnnotations.class);  

  15.             if (userNameAnnotations != null) {  

  16.                 System.out.println("value:" + userNameAnnotations.value());  

  17.             } else {  

  18.                 System.out.println("null");  

  19.             }  

  20.         } else {  

  21.             System.out.println("this is not Annotations class");  

  22.         }  

  23.   

  24.     }  

  25. }  


4. 输出:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. this is a Annotations class  

  2. value:initphp  



3. 创建一个自定义注解 - 作用于方法

1. 自定义注解类

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  2.   

  3.   

  4. import java.lang.annotation.Documented;  

  5. import java.lang.annotation.ElementType;  

  6. import java.lang.annotation.Retention;  

  7. import java.lang.annotation.RetentionPolicy;  

  8. import java.lang.annotation.Target;  

  9.   

  10. /** 

  11.  * 定义一个作用到方法的注解 

  12.  * @author zhuli.zhul 

  13.  * @date 2014-7-5 

  14.  */  

  15. @Documented//文档  

  16. @Retention(RetentionPolicy.RUNTIME)//在运行时可以获取  

  17. @Target({ ElementType.TYPE, ElementType.METHOD })//作用到类,方法,接口上等  

  18. public @interface MethodType {  

  19.   

  20.     //枚举类型  

  21.     public enum MethodTypeEnum {  

  22.         TYPE1, TYPE2  

  23.     }  

  24.   

  25.     //实际的值  

  26.     public MethodTypeEnum methodType() default MethodTypeEnum.TYPE1;  

  27. }  


2. 创建一个使用注解的类

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  2.   

  3. import com.test.www.MethodType.MethodTypeEnum;  

  4.   

  5.   

  6. /** 

  7.  * 一个注解的测试类 

  8.  * @author zhuli 

  9.  * @date 2014-7-5 

  10.  */  

  11. //注入注解作用于类上面  

  12. //可以通过反射 获取类的信息之后 获取得到这个注解的值  

  13. @UserNameAnnotations(value = "initphp")   

  14. public class Test {  

  15.   

  16.     private String userName;  

  17.   

  18.     //注解到  

  19.     @MethodType(methodType=MethodTypeEnum.TYPE2)  

  20.     public String getUserName() {  

  21.         return userName;  

  22.     }  

  23.   

  24.     public void setUserName(String userName) {  

  25.         this.userName = userName;  

  26.     }  

  27.   

  28. }  


3. 创建main入口

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  2.   

  3.   

  4. import java.lang.reflect.Method;  

  5.   

  6. import com.test.www.MethodType.MethodTypeEnum;  

  7.   

  8. public class mainTest {  

  9.   

  10.     public static void main(String[] args) {  

  11.         Class<Test> testClass = Test.class;  

  12.         try {  

  13.             //因为是注解到method上的,所以首先要获取这个方法  

  14.             Method method = testClass.getDeclaredMethod("getUserName");  

  15.               

  16.             //判断这个方法上是否有这个注解  

  17.             if (method.isAnnotationPresent(MethodType.class)) {  

  18.                 System.out.println("this is a method Annotation");  

  19.                   

  20.                 //如果有这个注解,则获取注解类  

  21.                 MethodType methodType = (MethodType) method.getAnnotation(MethodType.class);  

  22.                 if (methodType != null) {  

  23.                     if (MethodTypeEnum.TYPE1.equals(methodType.methodType())) {  

  24.                         System.out.println("this is TYPE1");  

  25.                     } else {  

  26.                         System.out.println("this is TYPE2");  

  27.                     }  

  28.                 }  

  29.             } else {  

  30.                 System.out.println("this is not  a method Annotation");  

  31.             }  

  32.   

  33.         } catch (Exception e) {  

  34.         }  

  35.   

  36.     }  

  37. }  


4. 输出:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. this is a method Annotation  

  2. this is TYPE2  



4. 创建一个自定义注解 - 作用于域

1. 创建一个自定义注解

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  2.   

  3.   

  4. import java.lang.annotation.Documented;  

  5. import java.lang.annotation.ElementType;  

  6. import java.lang.annotation.Retention;  

  7. import java.lang.annotation.RetentionPolicy;  

  8. import java.lang.annotation.Target;  

  9.   

  10. /** 

  11.  * 定义一个作用到域上的自定义注解 

  12.  * @author zhuli 

  13.  * @date 2014-7-5 

  14.  */  

  15. @Documented//文档  

  16. @Retention(RetentionPolicy.RUNTIME)//在运行时可以获取  

  17. @Target({ ElementType.FIELD })//作用到类的域上面  

  18. public @interface FieldAnnotations {  

  19.   

  20.     public String value() default ""//使用的时候 @FieldAnnotations(value="xxx")  

  21.   

  22. }  


2. 创建一个使用注解的类

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  2.   

  3. import com.test.www.MethodType.MethodTypeEnum;  

  4.   

  5.   

  6. /** 

  7.  * 一个注解的测试类 

  8.  * @author zhuli 

  9.  * @date 2014-7-5 

  10.  */  

  11. //注入注解作用于类上面  

  12. //可以通过反射 获取类的信息之后 获取得到这个注解的值  

  13. @UserNameAnnotations(value = "initphp")   

  14. public class Test {  

  15.   

  16.     @FieldAnnotations(value="zhuli")  

  17.     private String userName;  

  18.   

  19.     //注解到  

  20.     @MethodType(methodType=MethodTypeEnum.TYPE2)  

  21.     public String getUserName() {  

  22.         return userName;  

  23.     }  

  24.   

  25.     public void setUserName(String userName) {  

  26.         this.userName = userName;  

  27.     }  

  28.   

  29. }  


3. 创建main入口类

[java] view plaincopyprint?在CODE上查看代码片派生到我的代码片

  1. package com.test.www;  

  2.   

  3.   

  4. import java.lang.reflect.Field;  

  5.   

  6. public class mainTest {  

  7.   

  8.     public static void main(String[] args) {  

  9.         Test test = new Test();  

  10.         Class<Test> testClass = Test.class;  

  11.         try {  

  12.             //因为是注解到Field上的,所以首先要获取这个字段  

  13.             Field field = testClass.getDeclaredField("userName");  

  14.   

  15.             //判断这个Field上是否有这个注解  

  16.             if (field.isAnnotationPresent(FieldAnnotations.class)) {  

  17.                 System.out.println("this is a field Annotation");  

  18.   

  19.                 //如果有这个注解,则获取注解类  

  20.                 FieldAnnotations fieldAnnotations = (FieldAnnotations) field.getAnnotation(FieldAnnotations.class);  

  21.                 if (fieldAnnotations != null) {  

  22.                     //通过反射给私有变量赋值  

  23.                     field.setAccessible(true);  

  24.                     field.set(test, fieldAnnotations.value());  

  25.                     System.out.println("value:" + test.getUserName());  

  26.                 }  

  27.             } else {  

  28.                 System.out.println("this is not  a field Annotation");  

  29.             }  

  30.   

  31.         } catch (Exception e) {  

  32.         }  

  33.   

  34.     }  

  35. }  


4. 输出:

this is a field Annotation  

  1. value:zhuli  



转载于:https://my.oschina.net/u/1018220/blog/538706

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值