隨筆 - 81  文章 - 1033  trackbacks - 0
          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          在浮躁的年代里,我們進取心太切,患得患失;虛榮心太強,戰(zhàn)戰(zhàn)兢兢。一心爭強好勝,惟恐榜上無名。
          I think I can fly , and flying like a bird !
          程序員一名,已售出,缺貨中!

          我的郵件聯(lián)系方式

          用且僅用于MSN

          博客點擊率
          free web counter
          free web counter

          常用鏈接

          留言簿(36)

          隨筆檔案

          搜索

          •  

          積分與排名

          • 積分 - 187743
          • 排名 - 309

          最新評論

          閱讀排行榜

          評論排行榜

          在創(chuàng)建EJB組件時,必需提供一些定義,使得EJB組件使用一些服務(wù)例如:安全服務(wù),持久化服務(wù),事務(wù)服務(wù)。EJB容器可以提供這些服務(wù),這樣EJB只要實現(xiàn)業(yè)務(wù)邏輯就可以了。但是說到底EJB容器使用EJB組件的元數(shù)據(jù)來提供這些服務(wù),在以前EJB的元數(shù)據(jù)是以XML配置文件形式出現(xiàn)的,這些配置文件與EJB源文件是分開的。
          EJB的部署人員無法了解EJB本身的信息,如果EJB組件的創(chuàng)建者用注釋(Annotation)的方法將這些配置服務(wù)的信息和代碼放在一起,這樣EJB的部署者就可以了解EJB的信息,EJB的home接口可以使用Annotation自動生成,當(dāng)然到目前為止更好的是在簡單的Java Object上使用Annotations。

          一 什么是Annotation

          在已經(jīng)發(fā)布的JDK1.5(tiger)中增加新的特色叫 Annotation。Annotation提供一種機制,將程序的元素如:類,方法,屬性,參數(shù),本地變量,包和元數(shù)據(jù)聯(lián)系起來。這樣編譯器可以將元數(shù)據(jù)存儲在Class文件中。這樣虛擬機和其它對象可以根據(jù)這些元數(shù)據(jù)來決定如何使用這些程序元素或改變它們的行為。

          二 定義一個簡單的Annotation并使用它

          1.定義Annotation

          定義一個Annotation是什么簡單的,它采取的是類似于Interface的定義方式: “@+annotation類型名稱+(..逗號分割的name-value對...)”

          代碼
          1. //Example?1 ??
          2. ??
          3. package ?sz.starbex.bill.annotation; ??
          4. ??
          5. import ?java.lang.annotation.Retention; ??
          6. ??
          7. import ?java.lang.annotation.RetentionPolicy; ??
          8. ??
          9. import ?java.lang.annotation.Target; ??
          10. ??
          11. import ?java.lang.annotation.ElementType; ??
          12. ??
          13. @Retention (RetentionPolicy.RUNTIME) ??
          14. ??
          15. @Target (ElementType.METHOD) ??
          16. ??
          17. public ? @interface ?SimpleAnnotation?{ ??
          18. ??
          19. String?value(); ??
          20. ??
          21. }??

          @Retention這個meta-annotation表示我們創(chuàng)建的SimpleAnnotation這個Annotation將會存儲在Class文件中,并在java

          VM運行時加載它。@Target這個meta-annotation表示我們創(chuàng)建的SimplwAnnotation將會為描述方法,而@interface SimpleAnnotation是我們自定義的Annotation,它有一個成員叫value,返回值是String。

          2.使用Annotation

          代碼
          1. //Example?2 ??
          2. ??
          3. package ?sz.starbex.bill.annotation; ??
          4. ??
          5. import ?sz.starbex.bill.annotation.SimpleAnnotation; ??
          6. ??
          7. public ? class ?UsingSimpleAnnotation?{ ??
          8. ??
          9. @SimpleAnnotation (value= "Pass:This?method?will?Pass" ) //注意name=value的用法 ??
          10. ??
          11. public ? void ?pass(){ ??
          12. ??
          13. if ( 10 > 5 )?System.out.println( "測試通過" ); ??
          14. ??
          15. } ??
          16. ??
          17. @SimpleAnnotation ( "Fail:This?method?will?Fail" ) //注意name=value的用法 ??
          18. ??
          19. public ? void ?fail(){ ??
          20. ??
          21. if ( 10 < 5 )?System.out.println( "測試失敗" ); ??
          22. ??
          23. } ??
          24. ??
          25. }??

          一個Annotation用于程序元素(在本例中是method),在method方法之前用(@Annotation名稱(name=value,name=value.....)。在本例中是@SimpleAnnotation(value="Pass:This method will Pass")。每個annotation具有一個名字和成員個數(shù)>=0,當(dāng)只有一個單一的成員時,這個成員就是value。我們也可以這樣寫 @SimpleAnnotation("Fail:This method will Fail")。至此@SimpleAnnotation將Pass和Fail聯(lián)系起來了。

          3.在運行時訪問Annotation

          一旦Annotation與程序元素聯(lián)系起來,我們可以通過反射訪問它們并可以取得它們的值。我們使用一個新的interface:java.lang.reflect.AnnotatedElement。java.lang.reflect.AnnotatedElement接口中的方法有:

          a. boolean isAnnotationPresent(Class annotationType)

          如果指定類型的注釋存在于此元素上,則返回 true,否則返回 false。
          b. T getAnnotation(Class annotationType)

          如果存在該元素的指定類型的注釋,則返回這些注釋,否則返回 null。
          c. Annotation[] getAnnotations()

          返回此元素上存在的所有注釋。
          d. Annotation[] getDeclaredAnnotations()

          返回直接存在于此元素上的所有注釋。
          你要注意 isAnnotationPresent和getAnnotation方法,它們使用了Generics,請參考我的Java 范型的Blog。

          下面我們列出一些實現(xiàn)了AnnotatedElement 接口的類

          1. java.lang.reflect.AccessibleObject

          2. java.lang.Class

          3. java.lang.reflect.Constructor

          4. java.lang.reflect.Field

          5. java.lang.reflect.Method

          6. java.lang.Package

          下面的Example程序說明了如何在運行環(huán)境訪問Annotation

          代碼
          1. package ?sz.starbex.bill.annotation; ??
          2. ??
          3. import ?sz.starbex.bill.annotation.SimpleAnnotation; ??
          4. ??
          5. import ?java.lang.reflect.Method; ??
          6. ??
          7. public ? class ?SimpleAccessAnnotation?{ ??
          8. ??
          9. static ? void ?accessAnnotationTest(Class?usingAnnnotationClass){ ??
          10. ??
          11. try ?{ ??
          12. ??
          13. //Object?usingAnnnotationClass=Class.forName(usingAnnotationClassName).newInstance(); ??
          14. ??
          15. Method?[]?methods=usingAnnnotationClass.getDeclaredMethods(); //取得對方法 ??
          16. ??
          17. for (Method?method:methods){ ??
          18. ??
          19. System.out.println(method.getName()); ??
          20. ??
          21. SimpleAnnotation? ??
          22. ??
          23. simpleAnnotation=method.getAnnotation(SimpleAnnotation. class ); //得到方法的Annotation ??
          24. ??
          25. if (simpleAnnotation!= null ){ ??
          26. ??
          27. System.out.print(simpleAnnotation.value()+ "==" ); ??
          28. ??
          29. String?result=invoke(method,usingAnnnotationClass); ??
          30. ??
          31. System.out.println(result); ??
          32. ??
          33. } ??
          34. ??
          35. } ??
          36. ??
          37. }? catch ?(Exception?e)?{ ??
          38. ??
          39. //?TODO?Auto-generated?catch?block ??
          40. ??
          41. e.printStackTrace(); ??
          42. ??
          43. } ??
          44. ??
          45. } ??
          46. ??
          47. static ?String?invoke(Method?m,?Object?o)?{ ??
          48. ??
          49. String?result?=? "passed" ; ??
          50. ??
          51. try ?{ ??
          52. ??
          53. m.invoke(m, new ?Object[]{}); ??
          54. ??
          55. }? catch ?(Exception?e)?{ ??
          56. ??
          57. //?TODO?Auto-generated?catch?block ??
          58. ??
          59. result?=? "failed" ; ??
          60. ??
          61. } ??
          62. ??
          63. return ?result; ??
          64. ??
          65. } ??
          66. ??
          67. /** ?
          68. *?@param?args ?
          69. */ ??
          70. ??
          71. public ? static ? void ?main(String[]?args)?{ ??
          72. ??
          73. //?TODO?Auto-generated?method?stub ??
          74. ??
          75. accessAnnotationTest(UsingSimpleAnnotation. class ); ??
          76. ??
          77. } ??
          78. ??
          79. }??

          Java 中的Annotation的定義

          Java中的Annotation

          Java定義了幾個標(biāo)準(zhǔn)的meta-annotation,在新Package中java.lang.annotation 中包含了以下meta-annotation:

          meta-annotation 說明

          @Target

          1. annotation的target是一個被標(biāo)注的程序元素。target說明了annotation所修飾的對象范圍:annotation可被用于packages、types(類、接口、枚舉、annotation類型)、類型成員(方法、構(gòu)造方法、成員變量、枚舉值)、方法參數(shù)和本地變量(如循環(huán)變量、catch參數(shù))。在annotation類型的聲明中使用了target可更加明晰其修飾的目標(biāo)。

          meta-annotation 說明
          @Target 1. annotation的target是一個被標(biāo)注的程序元素。target說明了annotation所修飾的對象范圍:annotation可被用于packages、types(類、接口、枚舉、annotation類型)、類型成員(方法、構(gòu)造方法、成員變量、枚舉值)、方法參數(shù)和本地變量(如循環(huán)變量、catch參數(shù))。在annotation類型的聲明中使用了target可更加明晰其修飾的目標(biāo)。

          2. ElementType的定義

          TYPE// Class, interface, or enum (but not annotation)
          FIELD// Field (including enumerated values)

          METHOD// Method (does not include constructors)

          PARAMETER// Method parameter

          CONSTRUCTOR// Constructor

          LOCAL_VARIABLE// Local variable or catch clause

          ANNOTATION_TYPE// Annotation Types (meta-annotations)

          PACKAGE// Java package
          @Retention

          1. SOURCE//按照規(guī)定使用注釋,但是并不將它保留到編譯后的類文件中

          2. CLASS//將注釋保留在編譯后的類文件中,但是在運行時忽略它

          3. RUNTIME//將注釋保留在編譯后的類文件中,并在第一次加載類時讀取它

          @Documented Documented 表示注釋應(yīng)該出現(xiàn)在類的 Javadoc 中

          @Inherited 一個Annotation將被繼承

          三個標(biāo)準(zhǔn)的Annotation 在java.lang包中:

          @Deprecated 對不再使用的方法進行注釋
          @Override 指明注釋的方法覆蓋超類的方法
          @SuppressWarnings 阻止編譯器的警告,例:當(dāng)類型不安全時

          下例來說明這三個標(biāo)準(zhǔn)的Annotation:

          代碼
          1. package ?sz.starbex.bill.annotation; ??
          2. ??
          3. import ?java.util.ArrayList; ??
          4. ??
          5. import ?java.util.List; ??
          6. ??
          7. public ? class ?SimpleOverrideAnnotation?{ ??
          8. ??
          9. public ? static ? void ?main(String[]?args)?{ ??
          10. ??
          11. SimpleOverrideAnnotation?test?=? new ?SimpleOverrideAnnotation(); ??
          12. ??
          13. System.out.println(test.toString()); ??
          14. ??
          15. } ??
          16. ??
          17. @Override ??
          18. ??
          19. public ?String?toString()?{ ??
          20. ??
          21. return ? "自己的類自己輸出" ; ??
          22. ??
          23. } ??
          24. ??
          25. @Deprecated ??
          26. ??
          27. public ? void ?doSomething()?{ ??
          28. ??
          29. System.out.println( "方法已過時" ?); ??
          30. ??
          31. } ??
          32. ??
          33. @SuppressWarnings (value={ "unchecked" }) ??
          34. ??
          35. public ? void ?testSuppressWarnings(){ ??
          36. ??
          37. List?testList= new ?ArrayList(); ??
          38. ??
          39. testList.add( "KKKK" ); //沒有使用范型,類型不安全 ??
          40. ??
          41. } ??
          42. ??
          43. }??

          二、Annotation使用實例

          一個組合的Annotation,注釋類的

          a. 商標(biāo)Annotation

          代碼
          1. package ?sz.starbex.bill.annotation; ??
          2. ??
          3. public ? @interface ?Trademark?{ ??
          4. ??
          5. String?name(); ??
          6. ??
          7. String?owner(); ??
          8. ??
          9. }???

          b.License的annotation

          代碼
          1. package ?sz.starbex.bill.annotation; ??
          2. ??
          3. import ?java.lang.annotation.*; ??
          4. ??
          5. @Retention (RetentionPolicy.RUNTIME) ??
          6. ??
          7. @Target ({ElementType.TYPE,?ElementType.PACKAGE}) ??
          8. ??
          9. public ? @interface ?License?{ ??
          10. ??
          11. String?name(); ??
          12. ??
          13. String?notice(); ??
          14. ??
          15. boolean ?redistributable(); ??
          16. ??
          17. Trademark[]?trademarks(); ??
          18. ??
          19. }???

          c.測試類

          代碼
          1. package ?sz.starbex.bill.annotation; ??
          2. ??
          3. @License (name= "Bill" , ??
          4. ??
          5. notice= "許可證" , ??
          6. ??
          7. redistributable= true , ??
          8. ??
          9. trademarks={ @Trademark (name= "Mercedes" ,owner= "Swedish" ), ??
          10. ??
          11. @Trademark (name= "Daewoo" ,owner= "Korean" ) ??
          12. ??
          13. }? ??
          14. ??
          15. ) ??
          16. ??
          17. public ? class ?TestLicenseAnnotation?{ ??
          18. ??
          19. public ? static ? void ?main(String[]?args)?{ ??
          20. ??
          21. TestLicenseAnnotation?test= new ?TestLicenseAnnotation(); ??
          22. ??
          23. License?license=test.getClass().getAnnotation(License. class ); ??
          24. ??
          25. System.out.println( "License發(fā)放人:" +license.name()); ??
          26. ??
          27. System.out.println( "License注意事項:" +license.notice()); ??
          28. ??
          29. System.out.println( "License許可:" +license.redistributable()); ??
          30. ??
          31. Trademark?[]?marks=license.trademarks(); ??
          32. ??
          33. for (Trademark?mark:marks){ ??
          34. ??
          35. System.out.println( "商標(biāo)名稱:" +mark.name()); ??
          36. ??
          37. System.out.println( "商標(biāo)的使用者:" +mark.owner()); ??
          38. ??
          39. } ??
          40. ??
          41. } ??
          42. ??
          43. }? ??
          44. ??

          from:http://java.chinaitlab.com/EJB/519586_2.html



          posted on 2007-01-22 22:26 cresposhi 閱讀(1005) 評論(6)  編輯  收藏

          FeedBack:
          # re: 什么是Annotation?【轉(zhuǎn)載】 2007-02-01 14:39 梅穎
          轉(zhuǎn)的也坐哈沙發(fā),呵呵  回復(fù)  更多評論
            
          # re: 什么是Annotation?【轉(zhuǎn)載】 2007-02-01 14:57 施偉
          呵呵,看來你無處不在列!  回復(fù)  更多評論
            
          # re: 什么是Annotation?【轉(zhuǎn)載】 2007-02-01 16:21 梅穎
          嗚嗚。。  回復(fù)  更多評論
            
          # re: 什么是Annotation?【轉(zhuǎn)載】 2007-02-01 21:46 施偉
          哭什么啊,誰欺負你了啊?  回復(fù)  更多評論
            
          # re: 什么是Annotation?【轉(zhuǎn)載】 2007-02-02 15:14 梅穎
          同事。。。  回復(fù)  更多評論
            
          # re: 什么是Annotation?【轉(zhuǎn)載】 2007-02-02 20:41 施偉
          好,我等會幫你檄文討伐  回復(fù)  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 汕头市| 吉木乃县| 原平市| 甘谷县| 洛川县| 北海市| 嘉义市| 九江县| 河池市| 榆社县| 延边| 林甸县| 炉霍县| 尼勒克县| 屯门区| 南汇区| 民权县| 荥经县| 成都市| 定边县| 舟曲县| 蓝田县| 柯坪县| 盐山县| 甘德县| 碌曲县| 江永县| 柘城县| 曲周县| 陕西省| 泽州县| 额尔古纳市| 清镇市| 会理县| 连云港市| 思茅市| 邯郸市| 余干县| 龙井市| 盐源县| 西安市|