自定义注解_解析注解

1 自定义注解:
创建自定义注解,需要将该注解放在某一个包下,任意一个包下都行,不能没有包。


创建自定义注解通过:public @interface来创建。
如:public @interface Controller {
}


在自定义的注解上需要添加说明。告知是针对类的注解还是针对方法的注解还是针对属性的注解。
如:
@Target(ElementType.TYPE)
说明: ElementType.TYPE是针对类或接口的注解,
ElementType.METHOD是针对方法的注解,
ElementType.FIELD是针对属性的注解。


在自定义的注解上需要添加注解的范围,可以理解为注解的保留范围或有效期。
如:
@Retention(RetentionPolicy.RUNTIME)
说明是运行期有效。


2 最简单的注解:
===========完整的一个类或接口的注解如下:=============
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Controller {


   
}
===========完整的一个类或接口的注解如下:=============




针对上述例子进行测试:
@Controller
public class TestControl {
   public static void main(String[] args) {

if(TestControl.class.isAnnotationPresent(Controller.class)){
   System.out.println("有注解加载类");
}else{
   System.out.println("没有注解加载类");
}


   }


}
说明:TestControl.class.isAnnotationPresent(Controller.class)
是用来判断TestControl类是否有Controller注解的。如果有则返回true,如果没有则返回false


3 针对类注解,带有参数值。
如:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestUrl {


   String value();
}


测试:
@RequestUrl(value="/test1")
public class Test1Control {


   public static void main(String[] args) {

if(Test1Control.class.isAnnotationPresent(RequestUrl.class)){
   System.out.println("yes");
   Annotation annotation=Test1Control.class.getAnnotation(RequestUrl.class);
   Map<String,Object> map=new Test1Control().getAnnotationKeyAndValue(annotation);
   
}else{
   System.out.println("no");
}
   }
   
   public Map<String,Object> getAnnotationKeyAndValue(Annotation annotation){
Map<String,Object> map=new HashMap<String,Object>();

List annotationMethods=new ArrayList();
annotationMethods.add("equals");
annotationMethods.add("toString");
annotationMethods.add("hashCode");
annotationMethods.add("annotationType");

Method[] methods=annotation.getClass().getDeclaredMethods();//获取本类中的所有方法,不包括继承的
for(int i=0;i<methods.length;i++){
   Method method=methods[i];
   //System.out.println(method.getName());
   String methodName=method.getName();
   if(annotationMethods.contains(methodName)){
continue;
   }else{
try {
String value=(String) method.invoke(annotation, null);
   System.out.println(method.getName()+":"+value);
   map.put(method.getName(), value);
}
catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
}
   }
   
   
}

return map;
   }
}


说明:该测试时,判断是否有RequestUrl注解,同时得到RequestUrl注解的参数名和参数值
通过java反射。
Test1Control.class.isAnnotationPresent(RequestUrl.class)这是判断是否有某个注解。
Annotation annotation=Test1Control.class.getAnnotation(RequestUrl.class);这是得到某个注解。
得到注解后就可以进行判断,得到注解的参数名和参数值。


java反射中:getMethods()和getDeclaredMethods()的区别:
getMethods()是得到public类型的方法,也包括继承得到的public方法
getDeclaredMethods()是得到本类中的所有方法。但是不包括继承得到的方法。
故一般用getDeclaredMethods()方法来得到类中的所有方法。


4 针对方法注解,带有参数值:
如:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ActionMethod {


   String value();
}


测试:
@RequestUrl(value="/test2")
public class Test2Control {


   @ActionMethod(value="testAdd")
   public void testAdd(){


   }
   
   public static void main(String[] args) {
List annotationMethods=new ArrayList();
annotationMethods.add("equals");
annotationMethods.add("toString");
annotationMethods.add("hashCode");
annotationMethods.add("annotationType");

Method[] methods=Test2Control.class.getDeclaredMethods();
for(int i=0;i<methods.length;i++){
   Method method=methods[i];
   //System.out.println(method.getName());
   if(method.isAnnotationPresent(ActionMethod.class)){
Annotation annotation=method.getAnnotation(ActionMethod.class);
Map<String,Object> map=new Test1Control().getAnnotationKeyAndValue(annotation);
   }
}
   }
}


5 针对属性注解:
如:
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Auto {
   
}


测试:
public class Test3Control {


   @Auto
   private Service service;
   
   public static void main(String[] args) {
Field[] fields=Test3Control.class.getDeclaredFields();
for(int i=0;i<fields.length;i++){
   Field field=fields[i];
   System.out.println(field.getName());
   if(field.isAnnotationPresent(Auto.class)){
System.out.println("yes");
   }else{
System.out.println("no");
   }
}
   }
}


说明:ElementType.FIELD是表示用于属性上的注解。
      Test3Control.class.getDeclaredFields()这个是用于获取本类中的所有的属性。


!!!注意:
Field、Method、Class这三者都能用isAnnotationPresent方法和getAnnotation方法。用于判断是否有某个注解和得到该注解。


网址://!!!!注解的说明:
//  http://www.importnew.com/14479.html
//  http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html
//  http://zhidao.baidu.com/link?url=uhRqBPEyaX2F_8Ba8lARkwX9LNJZtgl1gL2LgtoOfff1afDhL28fqYS9HKpyPYOBwfDtQnbsbCxU_iKBh99VwwyoPf0WU-rfBGp7PIyIEYG
//  http://www.cnblogs.com/qqzy168/p/3622712.html
http://www.android100.org/html/201503/30/131452.html


综述:
注解可以用于类或接口、方法、属性。同时注解还能有参数。
注解还可以进行解析,以上示例以说明如何进行解析,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值