石建 | Fat Mind

          Annotation一點總結

          Annotation

           

          題記:建議關于spring問題,請記得查看spring reference

           

          一、annotation前生后世

          Annotations do not directly affect program semantics, but they do affect the way programs are treated by tools and libraries, which can in turn affect the semantics of the running program. Annotations can be read from source files, class files, or reflectively at run time.

          譯:annotation不會直接影響程序的語義,xxxAnnotation可以從源文件、class文件、通過反射在運行時讀取。

           

          參考:http://www.developer.com/print.php/3556176

          1.       定義

          Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the interface keyword. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause. Return types are restricted to primitives, StringClassenums, annotations, and arrays of the preceding types. Methods can have default values.

          Annotation聲明與普通的interface非常相似,在關鍵字interface前加@。每一個方法的聲明定義一個annotation的元素。方法不能有任何的參數或throws異常。返回類型被限制為:原始類型、StringClassenumannotation、前面描述的類型組成的數組。method定義允許有默認值。

           

          2.       Java annotation

          There are two types of annotations available with JDK5:

          1) Simple annotations: These are the basic types supplied with Tiger, which you can use to annotate your code only; you cannot use those to create a custom annotation type.

          三個基本的annotation,如:OverrideDeprecatedSuppresswarnings,不能使用它去定義新的annotation

          2) Meta annotations: These are the annotation types designed for annotating annotation-type declarations. Simply speaking, these are called the annotations-of-annotations.

          annotation,定義annotation的類型。如:TargetRetentionDocumentedInherited

          A.      Target:聲明annotation注解的目標類型。如@Target(ElementType.TYPE)@Target(ElementType.METHOD)

          B.      Retention:聲明annotation被保留的長度。如:RetentionPolicy.SOURCERetentionPolicy.CLASSRetentionPolicy.RUNTIME

          C.      Documented:聲明被注解的target生成doc是否需要顯示annotation信息。

          D.      Inheritedxxx

           

          3.       annotation作用

          a. 不影響程序原本語義的情況下,增加信息+工具=聲明式編程。如:spring aop

          b. 編譯檢查

           

           

          二、利用annotation實現aop

          1. 思路

          A.自定義注解定義規則(何時執行)

          B.標記行為(執行什么)

          C.通過反射生成代理,在對象執行任何方法時,進行攔截判斷是否符合規則;若符合,執行對應的行為。

          2. 例子

          場景:一個表演家,表演節目后,觀眾拍手鼓掌

          原始:表演家擁有所有觀眾的引用,在自己表演完后,通知觀眾鼓掌

          問題:表演家應該關注表演自身的事情,有哪些觀眾、觀眾是否鼓掌,不是其所關注的

          改進:AOP方式,表演家僅關注表演,觀眾鼓掌由其它人負責

          總結:

          面向切面編程 (AOP) 提供從另一個角度來考慮程序結構以完善面向對象編程(OOP)。 面向對象將應用程序分解成各個層次的對象,而AOP將程序分解成各個切面或者說關注點。這使得可以模塊化諸如事務管理等這些橫切多個對象的關注點。

           

          三、spring aop如何使用annotation

          1. 基本方式,通過ProxyFactoryBean

          a.通知

          b.切入點

          b.通知+切入點 à 切面

          c.實際對象+接口+切面 à 接口代理()

          2. 自動代理方式:

          基本方式的問題是,xml配置文件繁瑣。

          1)基于spring上下文的通知者Bean的自動代理(利用PostBeanProcessor ?)

          BeanNameAutoProxyCreator,有屬性:beanNames(被代理對象)、interceptorNames(通知)。自動代理beanNames指定的bean,此時interceptorNames指定通知,但interceptor需要實現特定的接口,如:MethodBeforeAdvice

          2) 基于aspectJ注解驅動

          使用aspectJ風格的注解。原理:生成代理,僅方法級別。使用AspectJ 提供的一個庫來做切點(pointcut)解析和匹配。

          4.       <aop:config>,優點:任何類都能轉化為切面,不需要特殊接口或注解,原始pojo對象

          5.       aop參數傳遞,見:spring reference 6.3.3.6 通知參數

          a. around切點,必須傳遞ProceedingJoinPoint參數,通過ProceedingJoinPoint取得方法的所有信息。

          b. 其它切點,利用JoinPoint取得方法的所有參數。

          c. 通過參數名綁定傳遞參數,未理解

           

           

          四、annotation相關技術

          1. cglib

          它的原理就是用Enhancer生成一個原有類的子類,并且設置好callbackproxy 則原有類的每個方法調用都會轉為調用實現了MethodInterceptor接口的proxyintercept() 函數。

          2.  asm

          ASM is an all purpose Java bytecode manipulation and analysis framework. It can be used to modify existing classes or dynamically generate classes, directly in binary form. Provided common transformations and analysis algorithms allow to easily assemble custom complex transformations and code analysis tools.

          ASM offer similar functionality as other bytecode frameworks, but it is focused on simplicity of use and performance. 

          簡單理解asm是比cglib更高級的code generate lib

           

          五、others問題

          1. Generics 

          Generics - This long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting. See theGenerics Tutorial. (JSR 14)

          泛型提供了編譯時類型檢查安全。這點很重要嗎

           

          2. 為什么spring aop aspectJ anonotationpointcut聲明,必須注解在方法上 ?通過方法唯一標識一個pointcut

          猜測:@PointcutTarget屬性指定僅為method。方法簽名成為pointcutid

            

           

          posted on 2011-06-25 20:09 石建 | Fat Mind 閱讀(333) 評論(0)  編輯  收藏 所屬分類: 一點理解

          導航

          <2011年6月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          統計

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          What 、How、Why,從細節中尋找不斷的成長點
          主站蜘蛛池模板: 喀什市| 金寨县| 南丹县| 荃湾区| 故城县| 高陵县| 博爱县| 司法| 水城县| 伊川县| 石楼县| 嘉黎县| 清水河县| 唐山市| 普兰店市| 清丰县| 江陵县| 桃源县| 贡觉县| 岳池县| 海阳市| 宁安市| 凉城县| 临猗县| 洛川县| 伊吾县| 峨眉山市| 竹山县| 高邑县| 公安县| 襄垣县| 临高县| 盐城市| 舒兰市| 安龙县| 竹北市| 百色市| 阿勒泰市| 佛山市| 班戈县| 荃湾区|