JAVA元数据注释初探

今天了解一下JAVA元数据的用法和一些作用,拿出一些例子,给自己看,怕自己以后忘了

JAVA的元数据功能是JDK1.5才开始支持的,以前都没有,正因为是新支持的,所以有关于它的介绍挺少,用处也不太多,最近发现很多框架都可以把它用来配置一些东西,以代替以前比较复杂的XML配置.想像一下,在JAVA代码中直接写入注释来配置,那该是多么好的事情,让我们写习惯了代码和看习惯了代码的人来说,这无疑是一件很爽的事情.

我们可以使用JAVA内置的注释内型,如果觉得不够用,可以定义自己的注释内型,定义如下
/*   
 * MyType.java   
 *   
 * Created on 2006年12月7日, 下午3:40   
 *   
 * To change this template, choose Tools | Template Manager   
 * and open the template in the editor.   
 */   
   
package testAnno;    
   
/**   
 *   
 * @author lbf   
 */   
import java.lang.annotation.*;    
@Retention(RetentionPolicy.RUNTIME)    
@Target({ElementType.TYPE,ElementType.METHOD})    
public @interface MyType {    
    String authorName();    
    String lastModified();    
    String bugFixes() default "ok";    
}

这里我定义了一个我自己的注释类,声明方式和声明接口差不多,只不过在interface前面多了一个@符号. 

注释类也可以用注释类注释,如此下去.

@Retention(RetentionPolicy.RUNTIME)

这句表示它的保存范围是到RUNTIME,也就是运行时,这样在类运行的时候,我们也可以取到有关它的信息.

@Target({ElementType.TYPE,ElementType.METHOD})  

这句表示它的适用对象,它可以用在哪里地方,我这里定义的是它可以用在类的定义和方法的定义上

然后我们看我们是怎么为我们写的类加上注释的

/*   
 * Test1.java   
 *   
 * Created on 2006年12月7日, 下午3:34   
 *   
 * To change this template, choose Tools | Template Manager   
 * and open the template in the editor.   
 */   
   
package testAnno;    
   
/**   
 *   
 * @author lbf   
 */   
import java.lang.annotation.*;    
@MyType(authorName="hadeslee",lastModified="20061207")    
public class Test1 {    
        
    /** Creates a new instance of Test1 */   
    public Test1() {    
    }    
    @Deprecated   
    @MyType(authorName="hadeslee",lastModified="20061207",bugFixes="what")    
    public void doSth(){    
               
    }    
    @MyType(authorName="hadeslee",lastModified="20061207",bugFixes="what")    
    public void doAnother(){    
            
    }    
}    

加了元数据的类和不加元数据的类差不多,只不过如果你的元数据注释如果是运行时的话,你的类文件可能会比不加元数据大一些,因为它必须把一些注释的信息写入到class文件中去,我们已经注释了我们的类,现在我们来看一下,我们如何去取我们的注释,
/*   
 * GetAnno.java   
 *   
 * Created on 2006年12月7日, 下午3:46   
 *   
 * To change this template, choose Tools | Template Manager   
 * and open the template in the editor.   
 */   
   
package testAnno;    
   
/**   
 *   
 * @author lbf   
 */   
import java.lang.annotation.*;    
import java.lang.reflect.*;    
public class GetAnno {    
        
    /** Creates a new instance of GetAnno */   
    public GetAnno() {    
    }    
    public static void main(String[] args)throws Exception {    
        Test1 t=new Test1();    
        Class c=Test1.class ;    
        Annotation[] as= c.getDeclaredAnnotations();    
        for(Annotation an:as){    
            System.out.println("类Test1的注释"+an);    
        }    
        Method med=c.getDeclaredMethod("doSth");    
        Annotation[] ass=med.getDeclaredAnnotations();    
        for(Annotation an:ass){    
            Class<!--/sp-->extends Annotation> clazz=an.annotationType();    
            Annotation[] ased=clazz.getAnnotations();    
            for(Annotation ad:ased){    
                System.out.println("注释的注释:"+ad);    
            }    
            System.out.println("方法doSth的注释:"+an);    
        }    
    }    
}   

此程序输出如下

类Test1的注释@testAnno.MyType(bugFixes=ok, authorName=hadeslee, lastModified=20061207)
注释的注释:@java.lang.annotation.Documented()
注释的注释:@java.lang.annotation.Retention(value=RUNTIME)
方法doSth的注释:@java.lang.Deprecated()
注释的注释:@java.lang.annotation.Retention(value=RUNTIME)
注释的注释:@java.lang.annotation.Target(value=[TYPE, METHOD])
方法doSth的注释:@testAnno.MyType(bugFixes=what, authorName=hadeslee, lastModified=20061207)

简单的写了一点点关元数据的东西,希望对自己或者对别人有一点点帮助.
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>

文章来自: 本站原创
引用通告地址: http://www.java125.cn/trackback.asp?tbID=1555
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值