如何使用自定义注解。

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

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


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

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

[java]  view plain copy
  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 plain copy
  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 plain copy
  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 plain copy
  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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值