如何使用自定义注解。

来源:互联网 发布:网络大电影商业计划书 编辑:程序博客网 时间:2024/05/29 09:51

Java注解目前广泛被应用。spring中的基于注解的依赖注入、Spring Web MVC中的Route、PlayFramework中的基于注解的Validation等。

使用注解,可以在适当地方替代XML的繁琐。


现在来看看,如何自定义注解。

目标:通过注解方式,定义属性的默认值。例如:

[java] view plaincopy
  1. public class DefaultUse {  
  2.       
  3.     @Default(value = "Hello Annotation")  
  4.     private String msg;  
  5.       
  6.     public void say(){  
  7.         System.out.println(msg);  
  8.     }  
  9. }  


一、新建Annotation

[java] view plaincopy
  1. import java.lang.annotation.ElementType;  
  2. import java.lang.annotation.Retention;  
  3. import java.lang.annotation.RetentionPolicy;  
  4. import java.lang.annotation.Target;  
  5.   
  6. /** 
  7.  * @Retention 指定注释的生存时期 
  8.  * CLASS:注释记录在类文件中,但在运行时 VM 不需要保留注释。 
  9.  * RUNTIME:注释记录在类文件中,在运行时 VM 将保留注释,因此可以使用反射机制读取注释内容。 
  10.  * SOURCE:编译器要丢弃的注释。 
  11.  */  
  12. @Retention(RetentionPolicy.RUNTIME)   
  13.   
  14. /** 
  15.  * @Target  
  16.  * 指示注释类型所适用的程序元素的种类,如果注释类型声明中不存在 Target 元注释, 
  17.  * 则声明的类型可以用在任一程序元素上。 
  18.  * ElementType.ANNOTATION_TYPE:注释类型声明 
  19.  * ElementType.CONSTRUCTOR:构造方法声明 
  20.  * ElementType.FILED:字段声明 
  21.  * ElementType.LOCAL_VARIABLE:局部变量声明 
  22.  * ElementType.METHOD:方法声明 
  23.  * ElementType.PACKAGE:包声明 
  24.  * ElementType.PARAMETER:参数声明 
  25.  * ElementType.TYPE:类、借口或枚举声明 
  26.  */  
  27. @Target(ElementType.FIELD)  
  28. public @interface Default {  
  29.     String value(); //默认值  
  30. }  

二、实际类中使用

[java] view plaincopy
  1. public class DefaultUse {  
  2.       
  3.     @Default(value = "Hello Annotation")  
  4.     private String msg;  
  5.       
  6.     public void setMsg(String msg) {  
  7.         this.msg = msg;  
  8.     }  
  9.   
  10.     public void say(){  
  11.         System.out.println(msg);  
  12.     }  
  13. }  

三、注解解析过程

[java] view plaincopy
  1. import java.beans.PropertyDescriptor;  
  2. import java.lang.reflect.Field;  
  3. import java.lang.reflect.Method;  
  4.   
  5. public class DefaultTest {  
  6.     public static void main(String args[]) throws Exception{  
  7.         DefaultUse use = new DefaultUse();  
  8.           
  9.         //Default注解的处理过程  
  10.         //这里使用反射机制完成默认值的设置  
  11.         Field[] fileds = use.getClass().getDeclaredFields();  
  12.           
  13.         for(Field filed : fileds){  
  14.             Default annotation = filed.getAnnotation(Default.class);  
  15.             if(annotation != null){  
  16.                 PropertyDescriptor pd = new PropertyDescriptor(filed.getName(), DefaultUse.class);  
  17.                 Method setterName = pd.getWriteMethod();  
  18.                   
  19.                 if(setterName!=null){  
  20.                     String value = annotation.value();  
  21.                     filed.setAccessible(true);  
  22.                     setterName.invoke(use, value);  
  23.                 }  
  24.             }  
  25.         }  
  26.           
  27.         use.say();  
  28.     }  
  29. }  
0 0
原创粉丝点击