zoukankan      html  css  js  c++  java
  • Java annotation

    Java annotation入门:http://www.blogjava.net/ericwang/archive/2005/12/13/23743.html

    Java annotation 手册: http://www.blogjava.net/mlh123caoer/archive/2007/09/06/143260.html

    Java annotation 高级应用: http://www.blogjava.net/wuxufeng8080/articles/93046.html

    试验代码:
     
    客户端代码TestAnnotaion,这个客户端可以是一个AOP的advisor,也可以是框架的Controlller
     

    package org.airlinkmatrix.sample;

    import java.lang.reflect.Method;

    import org.airlinkmatrix.sample.annotation.IsWired;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;

    @IsWired
    public class TestAnnotation {
     
     static private Log log = LogFactory.getLog(TestAnnotation.class);
     
     @IsWired(kidding="are you crazy?")
     private void wired(){
      
     }
     
     @IsWired public void wired1(int k){
      
     }
     public static void main(String[] args) {
      Class clazz = TestAnnotation.class;
      if (clazz.isAnnotationPresent(IsWired.class)){
       log.info("class is wired!");
      }
      
      try {
       Method[] methods = clazz.getMethods();
       for (Method method : methods) {
        if (method.isAnnotationPresent(IsWired.class)){
         log.info("the method " + method.getName() +
           " is wired!!");
        }else{
         log.info("the method " + method.getName() +
         " is not wired!!");
        }
       }
       
      } catch (SecurityException e) {
       e.printStackTrace();
      }
      
      
     }
    }

     
    标注的定义代码:
     
    package org.airlinkmatrix.sample.annotation;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    @Retention(RetentionPolicy.RUNTIME)
    public @interface IsWired {
     public String kidding() default "";
    }
     
    运行结果:
    TestAnnotation - class is wired!
    TestAnnotation - the method wired1 is wired!!
    TestAnnotation - the method main is not wired!!
    TestAnnotation - the method hashCode is not wired!!
    TestAnnotation - the method getClass is not wired!!
    TestAnnotation - the method wait is not wired!!
    TestAnnotation - the method wait is not wired!!
    TestAnnotation - the method wait is not wired!!
    TestAnnotation - the method equals is not wired!!
    TestAnnotation - the method notify is not wired!!
    TestAnnotation - the method notifyAll is not wired!!
    TestAnnotation - the method toString is not wired!!
     
     

     
    按照我的理解,annotation的本意就是允许开发者描述程序元素(类、方法、属性等等)的元数据,这样会衍生出多种应用。
    其一,就是代替以前的ad hoc annotation,例如XDoclect,transeint关键字,java doc某些标注,以及各种框架的配置中的某些内容(例如hibernate对PO的配置)。
    其二,这是我自己想到的应用,其实annotation可以用作一个弱契约的接口,可以间接的实现多继承,这是非常令人兴奋的用法。
    可能大家都知道interface vs abstract superclass的利弊考虑:用interface可以实现多继承,但麻烦之处在于必须每个“子类”都必须自己实现接口的方法,可能会有很多重复代码; 用abstract superclass可以提供缺省实现,子类可以不必重写该方法,但缺点是必须强迫子类显式地继承抽象父类,有时造成了不必要的侵入。
     
    类似于接口的使用,annotation可以约定实现某个方法(例如getMethodName())签名作为IoC回调方法。但这是个弱契约,编译器决不会检查和强制标注annotation的“子类”一定要实现annotation所约定的这个方法。框架或回调该“子类”的模板方法可以在子类没有提供getMethodName()的时候,由框架或模板方法自己提供缺省实现。   也就是说,annotaion达到了“声明即拥有”的奇效。  与interface不同,“子类”允许重写实现,但不必作强制实现,标注本身也不负责实现。相反,保证标注功能的实现的职责转移到了客户代码(框架或者模板方法)中。
     
    进一步学习中,请大家不吝赐教。
  • 相关阅读:
    UVa 122 Trees on the level
    UVa 623 500!
    UVa 424 Integer Inquiry
    UVa 10082 WERTYU
    关于c语言的输入输出
    (转)提问的智慧for oracle
    根据输入的用户ID串,返回用户名字串:TRIM函数的使用
    转:Oracle数据库一致性读的原理(Consistent Read)
    Instr()函数的使用计算字符串中出现某个字母或单词的个数
    RETURNING的使用:
  • 原文地址:https://www.cnblogs.com/kelin1314/p/1992209.html
Copyright © 2011-2022 走看看