AOP with Spring

Spring事務(wù)管理用AOP實(shí)現(xiàn),這里列一下APO的基本概念,AOP是建立在OOP基礎(chǔ)上對(duì)類的行為進(jìn)行歸類后,形成的另一種理解程序結(jié)構(gòu)的方法。以下內(nèi)容摘自Spring官方文檔窃这。其中切面瞳别、連接點(diǎn)、通知杭攻、切入點(diǎn)的概念比較重要,可自己根據(jù)英文解釋理解祟敛。連接點(diǎn)是程序執(zhí)行的點(diǎn),表示一個(gè)執(zhí)行的方法,切入點(diǎn)是根據(jù)條件對(duì)連接點(diǎn)進(jìn)行的篩選匹配,為保證原汁原味,下面附上原文

AOP concepts

Aspect: a modularization of a concern that cuts across multiple classes. Transaction management isa good example of a crosscutting concern in enterprise Java applications. In Spring AOP, aspectsare implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (the @AspectJ style).

Join point: a point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

?Advice: action taken by an aspect at a particular join point. Different types of advice include "around," "before" and "after" advice. (Advice types are discussed below.) Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors around the join point.

? Pointcut: a predicate that matches join points. Advice is associated with a pointcut expression and runs at any join point matched by the pointcut (for example, the execution of a method with a certain name). The concept of join points as matched by pointcut expressions is central to AOP, and Spring uses the AspectJ pointcut expression language by default.

? Introduction: declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example,you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

? Target object: object being advised by one or more aspects. Also referred to as the advised object.Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.

? AOP proxy: an object created by the AOP framework in order to implement the aspect contracts(advise method executions and so on). In the Spring Framework, an AOP proxy will be a JDK dynamic proxy or a CGLIB proxy.

? Weaving: linking aspects with other application types or objects to create an advised object. This can be done at compile time (using the AspectJ compiler, for example), load time, or at runtime. Spring AOP, like other pure Java AOP frameworks, performs weaving at runtime.


Types of advice:

? Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

? After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.

? After throwing advice: Advice to be executed if a method exits by throwing an exception.

? After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).

? Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception.

通知類型主要是根據(jù)連接點(diǎn)的執(zhí)行和異常拋出情況執(zhí)行通知,?Before advice?在連接點(diǎn)執(zhí)行之前執(zhí)行,After returning advice 在連接點(diǎn)正常返回后執(zhí),After throwing advice則是在異常拋出后執(zhí)行,After (finally) advice 是連接點(diǎn)執(zhí)行后無論異常與否都會(huì)執(zhí)行,Around advice是在連接點(diǎn)執(zhí)行前和執(zhí)行后都會(huì)執(zhí)行,Spring 建議根據(jù)需求選擇合適的最簡(jiǎn)單的通知類型。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末兆解,一起剝皮案震驚了整個(gè)濱河市馆铁,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌锅睛,老刑警劉巖埠巨,帶你破解...
    沈念sama閱讀 207,248評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異现拒,居然都是意外死亡辣垒,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門印蔬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來勋桶,“玉大人,你說我怎么就攤上這事侥猬±裕” “怎么了?”我有些...
    開封第一講書人閱讀 153,443評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵陵究,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我奥帘,道長(zhǎng)铜邮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,475評(píng)論 1 279
  • 正文 為了忘掉前任寨蹋,我火速辦了婚禮松蒜,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘已旧。我一直安慰自己秸苗,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,458評(píng)論 5 374
  • 文/花漫 我一把揭開白布运褪。 她就那樣靜靜地躺著惊楼,像睡著了一般玖瘸。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上檀咙,一...
    開封第一講書人閱讀 49,185評(píng)論 1 284
  • 那天雅倒,我揣著相機(jī)與錄音,去河邊找鬼弧可。 笑死蔑匣,一個(gè)胖子當(dāng)著我的面吹牛力奋,可吹牛的內(nèi)容都是我干的潮剪。 我是一名探鬼主播,決...
    沈念sama閱讀 38,451評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼腾夯,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼校套!你這毒婦竟也來了价脾?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,112評(píng)論 0 261
  • 序言:老撾萬榮一對(duì)情侶失蹤搔确,失蹤者是張志新(化名)和其女友劉穎彼棍,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體膳算,經(jīng)...
    沈念sama閱讀 43,609評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡座硕,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,083評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了涕蜂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片华匾。...
    茶點(diǎn)故事閱讀 38,163評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖机隙,靈堂內(nèi)的尸體忽然破棺而出蜘拉,到底是詐尸還是另有隱情,我是刑警寧澤有鹿,帶...
    沈念sama閱讀 33,803評(píng)論 4 323
  • 正文 年R本政府宣布旭旭,位于F島的核電站,受9級(jí)特大地震影響葱跋,放射性物質(zhì)發(fā)生泄漏持寄。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,357評(píng)論 3 307
  • 文/蒙蒙 一娱俺、第九天 我趴在偏房一處隱蔽的房頂上張望稍味。 院中可真熱鬧,春花似錦荠卷、人聲如沸模庐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,357評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽掂碱。三九已至怜姿,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間顶吮,已是汗流浹背社牲。 一陣腳步聲響...
    開封第一講書人閱讀 31,590評(píng)論 1 261
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留悴了,地道東北人搏恤。 一個(gè)月前我還...
    沈念sama閱讀 45,636評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像湃交,于是被迫代替她去往敵國(guó)和親熟空。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,925評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,294評(píng)論 0 10
  • 基本知識(shí) 其實(shí), 接觸了這么久的 AOP, 我感覺, AOP 給人難以理解的一個(gè)關(guān)鍵點(diǎn)是它的概念比較多, 而且坑爹...
    永順閱讀 8,100評(píng)論 5 114
  • 因?yàn)楣ぷ餍枨蟾爿海约喝チ私庖幌耡op并做下的記錄息罗,當(dāng)然大部分都是參考他人博客以及官方文檔。 目錄 [關(guān)于 AOP](...
    forip閱讀 2,267評(píng)論 1 20
  • 高考快要結(jié)束了挨摸,每到這個(gè)時(shí)候總能想起自己高三時(shí)那種勤奮的樣子,那時(shí)挑著夜燈岁歉,做著永遠(yuǎn)也做不完的試卷和題目得运,翻著永遠(yuǎn)...
    慵懶的小柒閱讀 357評(píng)論 2 7
  • 有時(shí)候熔掺,有時(shí)候 我會(huì)相信一切有盡頭, 相聚離開都有時(shí)候非剃, 沒有什么會(huì)永垂不朽置逻。 ----王菲 《紅豆》 離...
    某天雨天閱讀 318評(píng)論 0 1