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不同,“子类”允许重写实现,但不必作强制实现,标注本身也不负责实现。相反,保证标注功能的实现的职责转移到了客户代码(框架或者模板方法)中。
进一步学习中,请大家不吝赐教。
转自:http://blog.sina.com.cn/s/blog_72cf53f80100oj31.html
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/