今天了解一下JAVA元數(shù)據(jù)的用法和一些作用,拿出一些例子,給自己看,怕自己以后忘了
JAVA的元數(shù)據(jù)功能是JDK1.5才開始支持的,以前都沒有,正因?yàn)槭切轮С值?所以有關(guān)于它的介紹挺少,用處也不太多,最近發(fā)現(xiàn)很多框架都可以把它用來配置一些東西,以代替以前比較復(fù)雜的XML配置.想像一下,在JAVA代碼中直接寫入注釋來配置,那該是多么好的事情,讓我們寫習(xí)慣了代碼和看習(xí)慣了代碼的人來說,這無疑是一件很爽的事情.
我們可以使用JAVA內(nèi)置的注釋內(nèi)型,如果覺得不夠用,可以定義自己的注釋內(nèi)型,定義如下
* 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,也就是運(yùn)行時,這樣在類運(yùn)行的時候,我們也可以取到有關(guān)它的信息.
@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(){
}
}
加了元數(shù)據(jù)的類和不加元數(shù)據(jù)的類差不多,只不過如果你的元數(shù)據(jù)注釋如果是運(yùn)行時的話,你的類文件可能會比不加元數(shù)據(jù)大一些,因?yàn)樗仨毎岩恍┳⑨尩男畔懭氲絚lass文件中去,我們已經(jīng)注釋了我們的類,現(xiàn)在我們來看一下,我們?nèi)绾稳ト∥覀兊淖⑨?
* 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)
簡單的寫了一點(diǎn)點(diǎn)關(guān)元數(shù)據(jù)的東西,希望對自己或者對別人有一點(diǎn)點(diǎn)幫助.
從這代碼里,我們可以看出,取注釋其實(shí)很簡單,就是利用反射機(jī)制來取的.不過我們要特別注意到的一點(diǎn)是,我們不但可以取我們定義的類的注釋,也可以取注釋的注釋,我們這里只取到了MyType的注釋,其實(shí)還可以往下取,在取的過程中,我們可以看到這些元數(shù)據(jù)注釋類都用了哪些注釋:)
盡管千里冰封
依然擁有晴空
你我共同品味JAVA的濃香.