AOPjava
什麼是AOP?spring
Aspect Oriented Programming 面向切面編程。sql
AOP常見的地方:最多見的就是咱們不少框架當中用到的攔截器。編程
AOP的用途: 事務管理,安全檢查,日誌記錄,緩存,對象池管理等。緩存
AOP的基本概念安全
AOP從程序的運行角度考慮程序的流程,提取業務處理過程的切面,AOP面向的是程序運行過程當中的各個步驟,但願以更好的方式來組合程序運行過程的各個步驟。框架
AOP框架並不與特定的業務代碼耦合,AOP框架可以處理程序執行中的特定切入點(PointCut),而不與具體類耦合。代理
AOP框架的特色:日誌
#1,各步驟之間良好的隔離性。code
#2,源代碼無關性。
下面是AOP中的經常使用術語:
#1, 切面(Aspect):業務流程運行的某個特定步驟,也就是應用運行過程的關注點,關注點可能橫切多個對象,因此嚐嚐也
稱爲橫切關注點。
#2,鏈接點(Joinpoint):程序執行過程當中明確的點,如方法的調用,或者異常的拋出。Spring AOP中,鏈接點老是方法的調用。
#3,加強處理(Advice): AOP框架在特定的切入點執行的加強處理。處理有"around","before","after"等.
#4, 切入點(Pointcut): 能夠插入增長處理的鏈接點。簡而言之,當某個鏈接點知足指定要求時,該鏈接點將被添加加強處理,該鏈接點也就變成了切入點。
#5,引入:將方法或者字段添加到被處理的類中。Spring容許引入新的接口到任何被處理的對象。
#6,目標對象: 被AOP框架進行加強處理的對象,也被稱爲被加強的對象。若是AOP框架是經過運行時代理來實現的,那麼這個對象將是一個被代理的對象。
#7, AOP代理:AOP框架建立的對象,簡單的說,代理就是對目標對象的增強。Spring中的AOP代理能夠是JDK的動態代理,也能夠是CGLIB代理。前者爲實現接口的目標對象的代理,後者爲不實現接口的目標對象的代理。
#8, 織入(Weaving):將加強處理添加到目標對象中,並建立一個被加強的對象(AOP代理)的過程就是織入。織入有兩種實現方式:編譯時加強和運行時加強。Spring和其餘純Java AOP框架同樣,在運行時完成織入。
Spring的AOP支持
Spring中的AOP代理由Spring的IOC容器負責生成,管理,其依賴關係也由IOC容器負責管理。所以,AOP代理能夠直接使用容器中的其餘Bean實例做爲目標,這種關係可由IOC容器的依賴注入提供。Spring默認使用Java動態代理來建立AOP代理,這樣就能夠爲任何接口實例建立代理了。
Spring也可使用CGLIB代理,在須要代理類而不是代理接口的時候,Spring自動會切換爲使用CGLIB代理。但Spring推薦使用面向接口變成,所以業務對象一般都會實現一個或多個接口,此時默認將使用JDK動態代理,但也能夠強制使用CGLIB。
package com.wangbiao.aspect; import java.sql.Date; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.core.annotation.Order; @Aspect @Order(value=2) public class LoginAspect { @Before("execution(* com.wangbiao.target.imp.*.*(..))") public void authority() { System.out.println("模擬執行權限檢查"); } // // @AfterReturning(returning ="rvt",pointcut="execution(* com.wangbiao.target.imp.*.*(..))") // public void log(String rvt) // { // System.out.println("記錄log " + rvt); // } @AfterReturning(pointcut="execution(* com.wangbiao.target.imp.*.*(..)) && args(name)") public void log2(String name) { System.out.println("記錄log " + name); } @AfterReturning(pointcut="execution(* com.wangbiao.target.imp.*.*(..)) && args(name,birthday)") public void log3(String name,Date birthday) { System.out.println("記錄log "); } // // @AfterThrowing(throwing ="exp",pointcut="execution(* com.wangbiao.target.imp.*.*(..))") // public void handleException(Throwable exp) // { // System.out.println("處理異常 " + exp.getCause()+"--"+exp.getMessage()); // } // //// @AfterThrowing(throwing ="exp",pointcut="execution(* com.wangbiao.target.imp.*.*(..))") //// public void handleException(NullPointerException exp) //// { //// System.out.println("處理異常 " + exp.getCause()+"--"+exp.getMessage()); //// } // // @After("execution(* com.wangbiao.target.imp.*.*(..))") // public void handleExceptionII() // { // System.out.println("處理異常 "); // } // // @Around("execution(* com.wangbiao.target.imp.*.*(..))") // public void remark(ProceedingJoinPoint pj) throws Throwable // { // System.out.println("XXXXXXXXXXXX---before"); // System.out.println(pj.getTarget()+"--"+pj.getSignature().getName()+"--"+pj.getArgs()); // pj.proceed(); // //pj.proceed(new Object[]{"hee"}); // System.out.println("XXXXXXXXXXXX---after"); // } }