<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    石建 | Fat Mind

    Annotation一點(diǎn)總結(jié)

    Annotation

     

    題記:建議關(guān)于spring問(wèn)題,請(qǐng)記得查看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不會(huì)直接影響程序的語(yǔ)義,xxxAnnotation可以從源文件、class文件、通過(guò)反射在運(yùn)行時(shí)讀取。

     

    參考: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非常相似,在關(guān)鍵字interface前加@。每一個(gè)方法的聲明定義一個(gè)annotation的元素。方法不能有任何的參數(shù)或throws異常。返回類(lèi)型被限制為:原始類(lèi)型、StringClassenumannotation、前面描述的類(lèi)型組成的數(shù)組。method定義允許有默認(rèn)值。

     

    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.

    三個(gè)基本的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的類(lèi)型。如:TargetRetentionDocumentedInherited

    A.      Target:聲明annotation注解的目標(biāo)類(lèi)型。如@Target(ElementType.TYPE)@Target(ElementType.METHOD)

    B.      Retention:聲明annotation被保留的長(zhǎng)度。如:RetentionPolicy.SOURCERetentionPolicy.CLASSRetentionPolicy.RUNTIME

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

    D.      Inheritedxxx

     

    3.       annotation作用

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

    b. 編譯檢查

     

     

    二、利用annotation實(shí)現(xiàn)aop

    1. 思路

    A.自定義注解定義規(guī)則(何時(shí)執(zhí)行)

    B.標(biāo)記行為(執(zhí)行什么)

    C.通過(guò)反射生成代理,在對(duì)象執(zhí)行任何方法時(shí),進(jìn)行攔截判斷是否符合規(guī)則;若符合,執(zhí)行對(duì)應(yīng)的行為。

    2. 例子

    場(chǎng)景:一個(gè)表演家,表演節(jié)目后,觀眾拍手鼓掌

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

    問(wèn)題:表演家應(yīng)該關(guān)注表演自身的事情,有哪些觀眾、觀眾是否鼓掌,不是其所關(guān)注的

    改進(jìn):AOP方式,表演家僅關(guān)注表演,觀眾鼓掌由其它人負(fù)責(zé)

    總結(jié):

    面向切面編程 (AOP) 提供從另一個(gè)角度來(lái)考慮程序結(jié)構(gòu)以完善面向?qū)ο缶幊蹋?/span>OOP)。 面向?qū)ο髮?yīng)用程序分解成各個(gè)層次的對(duì)象,而AOP將程序分解成各個(gè)切面或者說(shuō)關(guān)注點(diǎn)。這使得可以模塊化諸如事務(wù)管理等這些橫切多個(gè)對(duì)象的關(guān)注點(diǎn)。

     

    三、spring aop如何使用annotation

    1. 基本方式,通過(guò)ProxyFactoryBean

    a.通知

    b.切入點(diǎn)

    b.通知+切入點(diǎn) à 切面

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

    2. 自動(dòng)代理方式:

    基本方式的問(wèn)題是,xml配置文件繁瑣。

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

    BeanNameAutoProxyCreator,有屬性:beanNames(被代理對(duì)象)、interceptorNames(通知)。自動(dòng)代理beanNames指定的bean,此時(shí)interceptorNames指定通知,但interceptor需要實(shí)現(xiàn)特定的接口,如:MethodBeforeAdvice

    2) 基于aspectJ注解驅(qū)動(dòng)

    使用aspectJ風(fēng)格的注解。原理:生成代理,僅方法級(jí)別。使用AspectJ 提供的一個(gè)庫(kù)來(lái)做切點(diǎn)(pointcut)解析和匹配。

    4.       <aop:config>,優(yōu)點(diǎn):任何類(lèi)都能轉(zhuǎn)化為切面,不需要特殊接口或注解,原始pojo對(duì)象

    5.       aop參數(shù)傳遞,見(jiàn):spring reference 6.3.3.6 通知參數(shù)

    a. around切點(diǎn),必須傳遞ProceedingJoinPoint參數(shù),通過(guò)ProceedingJoinPoint取得方法的所有信息。

    b. 其它切點(diǎn),利用JoinPoint取得方法的所有參數(shù)。

    c. 通過(guò)參數(shù)名綁定傳遞參數(shù),未理解

     

     

    四、annotation相關(guān)技術(shù)

    1. cglib

    它的原理就是用Enhancer生成一個(gè)原有類(lèi)的子類(lèi),并且設(shè)置好callbackproxy 則原有類(lèi)的每個(gè)方法調(diào)用都會(huì)轉(zhuǎn)為調(diào)用實(shí)現(xiàn)了MethodInterceptor接口的proxyintercept() 函數(shù)。

    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. 

    簡(jiǎn)單理解asm是比cglib更高級(jí)的code generate lib

     

    五、others問(wèn)題

    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)

    泛型提供了編譯時(shí)類(lèi)型檢查安全。這點(diǎn)很重要嗎

     

    2. 為什么spring aop aspectJ anonotationpointcut聲明,必須注解在方法上 ?通過(guò)方法唯一標(biāo)識(shí)一個(gè)pointcut

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

      

     

    posted on 2011-06-25 20:09 石建 | Fat Mind 閱讀(332) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 一點(diǎn)理解

    導(dǎo)航

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

    統(tǒng)計(jì)

    常用鏈接

    留言簿

    隨筆分類(lèi)

    隨筆檔案

    搜索

    最新評(píng)論

    What 、How、Why,從細(xì)節(jié)中尋找不斷的成長(zhǎng)點(diǎn)
    主站蜘蛛池模板: 久久精品成人免费国产片小草| 最近的中文字幕大全免费8| 亚洲精品无码不卡在线播放HE| 57pao国产成永久免费视频| 亚洲乱码中文字幕在线| 亚洲中文久久精品无码| 18禁网站免费无遮挡无码中文| 婷婷亚洲综合五月天小说在线| 亚洲国产精品无码专区影院| 老司机在线免费视频| 精品免费久久久久国产一区 | 在线免费观看h片| 亚洲最大中文字幕| 无码不卡亚洲成?人片| 最近2019年免费中文字幕高清| 曰批全过程免费视频观看免费软件 | 国产精品四虎在线观看免费 | 四虎精品视频在线永久免费观看| 朝桐光亚洲专区在线中文字幕 | 不卡一卡二卡三亚洲| 久久久久久曰本AV免费免费| 一区二区三区免费在线视频 | 久久亚洲免费视频| 国产亚洲福利一区二区免费看| 69视频在线是免费观看| 和老外3p爽粗大免费视频| 亚洲国产成人99精品激情在线| 亚洲乱码中文字幕久久孕妇黑人 | 亚洲无人区一区二区三区| 四虎影院免费在线播放| 91香焦国产线观看看免费| 一级毛片免费视频网站| 亚洲午夜成人精品无码色欲| 亚洲AV日韩精品久久久久久| 亚洲国产日韩在线观频| 久久久久久久久免费看无码| 午夜免费福利小电影| 9久热这里只有精品免费| 黄色片网站在线免费观看| 在线亚洲高清揄拍自拍一品区| 亚洲bt加勒比一区二区|