Spring中AOP的理解

1.AOP的概念

         AOP(AspectOriented Programming,面向切面編程)指的是可以通過預編譯方式和運行期動態代理實現在不修改源代碼的情況下個程序動態統一添加功能的一種技術。AOP提供從一個角度來考慮程序結構以完善面向對象編程。它爲開發人員提供了一種描述橫切關注點的機制,並能夠自動將橫向關注點入到面向對象的軟件系統中,從而實現了橫切關注點模塊化

         AOP一般用於實現那些與業務無關,但是業務模塊所共同調用的邏輯,例如異常攔截、事務管理、日誌管理和權限控制等,AOP將這些代碼封裝起來,有利於減少系統的重複代碼,降低模塊間耦合度,提供可維護性。

         AOP的基本概念:Aspect(切面):一個關注點的模塊化,這個關注點可能會橫切多個對象;Joinpoint(連接點):程序執行過程中某個特定的點,比如處理異常的時候;Advice(通知):在切面的某個特定連接點上執行的操作;Pointcut(切入點):匹配連接點的斷言;Introduction(引入):用來給一個類型聲明額外的方法和屬性;Target Object(目標對象):被一個或多個切面所統治的對象;Weaving(織入):被切面連接到其他應用程序類型或對象上,並創建被通知的對象。

         2.AOP的實現原理

         AOP的核心實現與IoC一樣依賴於Java的反射機制,但是與IoC不同的地方是,IoC用到的相關設計模式是工程模式,而AOP用到的是代理模式。代理模式的思想是不讓外部直接訪問具體目標,而是需要通過一個代理類才能訪問,所有與具體目標的通信都能通過這道“隔離層”來傳送請求或返回狀態。

         3.AOP的通知類型

/*

         1、Before advice(前置通知):在方法執行之前執行,在一個切面裏使用@Before

         註解聲明通知。配置參考示例如下:

*/

<bean id="afterReturningAdviceExample" class="完整類名" />

         <aop:config proxy-target-class="false">

                   <!-- 定義切入點 -->

                   <aop:pointcut type="aspectj" id="beforePointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定義切面 -->

                   <aop:aspect ref="beforeAdviceExample">

                            <aop:before pointcut-ref="beforePointcut" method="beforeAspect" />

                   </aop:aspect>

         </aop:config>

/*

         execution(*com.itjob.spring.aop..*Manager.(..))表示切入點爲com.itjob.spring.aop

         包及其子包中以Manager結尾的類的任意方法錢作爲切入點

*/

/*

         2、After returning advice(返回後通知):通常在已匹配的方法返回的時候執行,

         也可使用@AfterReturning註釋來聲明。配置如下

*/

<bean id="afterReturningAdviceExample" class="完整類名" />

         <aop:config proxy-target-class="false">

                   <!-- 定義切入點 -->

                   <aop:pointcut type="aspectj" id="afterReturningPointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定義切面 -->

                   <aop:aspect ref="afterReturningAdviceExample">

                            <aop:after-returning pointcut-ref="afterReturningPointcut"

                                     method="afterReturningAspect" returning="returnValue"/>

                   </aop:aspect>

         </aop:config>

/*

         3、After throwing advice(拋出後通知):在一個方法拋出異常後執行。採用聲明

         方式定義切入點時,可使用@AfterThrowing註釋來聲明,配置入下

*/

<bean id="afterThrowingAdviceExample" class="完整類名" />

         <aop:config proxy-target-class="false">

                   <!-- 定義切入點 -->

                   <aop:pointcut type="aspectj" id="afterThrowingPointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定義切面 -->

                   <aop:aspect ref="afterThrowingAdviceExample">

                            <aop:after-throwing pointcut-ref="afterThrowingPointcut"

                                     method="afterThrowingAspect" throwing="ex"/>

                   </aop:aspect>

         </aop:config>

/*

         4、After (finally)advice(後通知):不論一個方法是如何結束的,在它結束後

         通知都會運行。若使用聲明式,可使用@After註釋來聲明。這個通知必須處理

         正常返回和異常返回兩種情況,通常來釋放資源,配置如下:

*/

<bean id="afterAdviceExample" class="完整類名" />

         <aop:config proxy-target-class="false">

                   <!-- 定義切入點 -->

                  <aop:pointcut type="aspectj" id="afterPointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定義切面 -->

                   <aop:aspect ref="afterAdviceExample">

                            <aop:after pointcut-ref="afterPointcut"

                                     method="afterAspect" throwing="ex"/>

                   </aop:aspect>

         </aop:config>

/*

         5、Around Advice(環繞通知):在一個方法執行之前和之後執行,並且它可以決定

         這個方法在什麼時候執行、如何執行、甚至是是否執行。聲明式的環繞通知可用

         @Around註釋來聲明。

*/

<bean id="aroundAdviceExample" class="完整類名" />

         <aop:config proxy-target-class="false">

                   <!-- 定義切入點 -->

                   <aop:pointcut type="aspectj" id="aroundPointcut"

                            exception="execution(*com.itjob.spring.aop..*Manager.*(..))"/>

                   <!-- 定義切面 -->

                   <aop:aspect ref="aroundAdviceExample">

                            <aop:after pointcut-ref="aroundPointcut"

                                     method="aroundAspect" />

                   </aop:aspect>

         </aop:config>

         4.AOP適用場景

         1)系統權限管理;2)錯誤處理和異常攔截;3)事務管理;4)持久化;5)同步問題;6)系統調試;7)性能優化;8)日誌

發佈了40 篇原創文章 · 獲贊 58 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章