1.1 AOP概述 1.1.1 什么是AOP? AOP : 全稱是Aspect Oriented Progamming既 : 面向切面編程.通過預編譯方式和運行期動態代理實作程式功能的統一維護的一種技術. 簡單的說它就是把我們程式重復的代碼抽取出來,在需要執行的時候,使用動態代理的技術,在不修改原始碼的基礎上,對我們的已有方法進行增強. 1.1.2 AOP的作用及優勢 作用 : 在程式運行期間,不修改原始碼對已有方法進行增強. 優勢 : 減少重復代碼,提高開發效率,維護方便. 1.1.3 AOP的實作方式 : 使用動態代理技術 1.2.2.1 動態代理的特點 : 位元組碼隨用隨創建,隨用隨加載. 它與靜態代理的區別也在于此.因為靜態代理是位元組碼一上來就創建好,并完成加載. 裝飾著模式就是靜態代理的一種體現. 1.2.2.2 動態代理常用的有兩種方式 基于介面的動態代理 提供者 : JDK官方的Proxy類. 要求 : 被代理來最少實作一個介面. 基于子類的動態代理 提供者 : 第三方的CGLib,如果報asmxxxx例外,需要匯入asm.jar. 要求 : 被代理類不能用final修飾的類(最終類). 1.3 Spring中的AOP 1.3.1 關于代理的選擇 在Spring中,框架根據目標類實作了介面決定采用哪種動態代理的方式. 1.3.2 AOP相關術語 Joinpoint(連接點) : 所謂連接點是指那些被攔截到的點.在Spring中,這些點值得是方法,因為spring只支持方法型別的連接點. Pointcut(切入點) : 所謂切入點是指我們要對那些Joinpoint進行攔截的定義. Advice(通知/增強) : 所謂通知是指攔截到Joinpoint之后所要做的事情就是通知. 通知的型別 : 前置通知,后置通知,例外通知,最終通知,環繞通知. Introduction(引介) : 是一種特殊的通知在不修改類代碼的前提下,Introduction可以在運行期為類動態地添加一些方法或Field. Target(目的物件) : 代理的目標物件. Weaving(織入) : 是值把增強應用到目標物件來創建新的代理物件的程序. spring采用動態代理織入,而AspectJ采用編譯器織入和類轉載期織入. Proxy (代理) : 一個類被AOP織入增強后,就是產生一個結果代理類. Aspect(切面) : 是切入點和通知(引介) 的結合. 1.4 基于XML的AOP配置 1.4.1 匯入包 1.4.2 準備介面 1.4.3 創建組態檔匯入約束 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> </beans> 2.1.4第四步:把客戶的業務層配置到spring容器中 <!-- 把資源交給spring來管理 --> <bean id="customerService" class="com.baidu.service.impl.CustomerServiceImpl"/> 2.1.5第五步:制作通知(增強的類) /** * 一個記錄日志的工具類 */ public class Logger { /** * 期望:此方法在業務核心方法執行之前,就記錄日志 */ public void beforePrintLog(){ System.out.println("Logger類中的printLog方法開始記錄日志了,,,,"); } } 2.2配置步驟 2.2.1第一步:把通知類用bean標簽配置起來 <!-- 把有公共代碼的類也讓spring來管理(把通知類也交給spring來管理) --> <bean id="logger" class="com.baidu.util.Logger"></bean> 2.2.2第二步:使用aop:config宣告aop配置 <!-- aop的配置 --> <aop:config> <!-- 配置的代碼都寫在此處 --> </aop:config> 2.2.3第三步:使用aop:aspect配置切面 <!-- 配置切面 :此標簽要出現在aop:config內部 id:給切面提供一個唯一標識 ref:參考的是通知類的bean的id --> <aop:aspect id="logAdvice" ref="logger"> <!--配置通知的型別要寫在此處--> </aop:aspect> 2.2.4第四步:使用aop:before配置前置通知 <!-- 用于配置前置通知:指定增強的方法在切入點方法之前執行 method:用于指定通知類中的增強方法名稱 ponitcut-ref:用于指定切入點的運算式的參考 --> <aop:before method="beforePrintLog" pointcut-ref="pt1"/> 2.2.5第五步:使用aop:pointcut配置切入點運算式 <aop:pointcut expression="execution(public void com.baidu.service.impl.CustomerServiceImpl.saveCustomer())" id="pt1"/> 2.3切入點運算式說明 execution: 匹配方法的執行(常用) execution(運算式) 運算式語法:execution([修飾符] 回傳值型別 包名.類名.方法名(引數)) 寫法說明: 全匹配方式: public void com.baidu.service.impl.CustomerServiceImpl.saveCustomer() 訪問修飾符可以省略 void com.baidu.service.impl.CustomerServiceImpl.saveCustomer() 回傳值可以使用*號,表示任意回傳值 * com.baidu.service.impl.CustomerServiceImpl.saveCustomer() 包名可以使用*號,表示任意包,但是有幾級包,需要寫幾個* * *.*.*.*.CustomerServiceImpl.saveCustomer() 使用..來表示當前包,及其子包 * com..CustomerServiceImpl.saveCustomer() 類名可以使用*號,表示任意類 * com..*.saveCustomer() 方法名可以使用*號,表示任意方法 * com..*.*() 引數串列可以使用*,表示引數可以是任意資料型別,但是必須有引數 * com..*.*(*) 引數串列可以使用..表示有無引數均可,有引數可以是任意型別 * com..*.*(..) 全通配方式: * *..*.*(..) 2.4常用標簽 2.4.1<aop:config> 作用: 用于宣告開始aop的配置 2.4.2<aop:aspect> 作用: 用于配置切面, 屬性: id:給切面提供一個唯一標識, ref:參考配置好的通知類bean的id, 2.4.3<aop:pointcut> 作用: 用于配置切入點運算式 屬性: expression:用于定義切入點運算式, id:用于給切入點運算式提供一個唯一標識, 2.4.4<aop:before> 作用: 用于配置前置通知 屬性: method:指定通知中方法的名稱, pointct:定義切入點運算式 pointcut-ref:指定切入點運算式的參考 2.4.5<aop:after-returning> 作用: 用于配置后置通知 屬性: method:指定通知中方法的名稱, pointct:定義切入點運算式 pointcut-ref:指定切入點運算式的參考 2.4.6<aop:after-throwing> 作用: 用于配置例外通知 屬性: method:指定通知中方法的名稱, pointct:定義切入點運算式 pointcut-ref:指定切入點運算式的參考 2.4.7<aop:after> 作用: 用于配置最終通知 屬性: method:指定通知中方法的名稱, pointct:定義切入點運算式 pointcut-ref:指定切入點運算式的參考 2.4.8<aop:around> 作用: 用于配置環繞通知 屬性: method:指定通知中方法的名稱, pointct:定義切入點運算式 pointcut-ref:指定切入點運算式的參考 2.5通知的型別 2.5.1型別說明 <!-- 配置通知的型別 aop:before: 用于配置前置通知,前置通知的執行時間點:切入點方法執行之前執行 aop:after-returning: 用于配置后置通知,后置通知的執行時間點:切入點方法正常執行之后,它和例外通知只能有一個執行 aop:after-throwing 用于配置例外通知,例外通知的執行時間點:切入點方法執行產生例外后執行,它和后置通知只能執行一個, aop:after 用于配置最終通知,最終通知的執行時間點:無論切入點方法執行時是否有例外,它都會在其后面執行, aop:around 用于配置環繞通知,他和前面四個不一樣,他不是用于指定通知方法何時執行的, --> <aop:before method="beforePrintLog" pointcut-ref="pt1"/> <aop:after-returning method="afterReturningPrintLog" pointcut-ref="pt1"/> <aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pt1"/> <aop:after method="afterPrintLog" pointcut-ref="pt1"/> <aop:around method="aroundPringLog" pointcut-ref="pt1"/> 2.5.2環繞通知的特殊說明 /** * 環繞通知 * 它是spring框架為我們提供的一種可以在代碼中手動控制增強部分什么時候執行的方式, * 問題: * 當我們配置了環繞通知之后,增強的代碼執行了,業務核心方法沒有執行, * 分析: * 通過動態代理我們知道在invoke方法中,有明確呼叫業務核心方法:method.invoke(), * 我們配置的環繞通知中,沒有明確呼叫業務核心方法, * 解決: * spring框架為我們提供了一個介面:ProceedingJoinPoint,它可以作為環繞通知的方法引數 * 在環繞通知執行時,spring框架會為我們提供該介面的實作類物件,我們直接使用就行, * 該介面中有一個方法proceed(),此方法就相當于method.invoke() */ public void aroundPringLog(ProceedingJoinPoint pjp){ try { System.out.println("前置通知:Logger類的aroundPringLog方法記錄日志"); pjp.proceed(); System.out.println("后置通知:Logger類的aroundPringLog方法記錄日志"); } catch (Throwable e) { System.out.println("例外通知:Logger類的aroundPringLog方法記錄日志"); e.printStackTrace(); }finally{ System.out.println("最終通知:Logger類的aroundPringLog方法記錄日志"); } } 3.1 基于注解的AOP配置 3.1.1 環境搭建 準備業務層和介面并用注解配置 3.1.2 匯入jar包 3.1.3 創建spring的組態檔并匯入約束 3.1.4 第四步:把資源使用注解讓spring來管理 3.1.5 第五步 : 在組態檔中指定spring要掃描的包 <-- 告知spring,在創建容器時要掃描的包--> <context:component-scan base-package="com.baidu"></context:component-scan> 3.2 配置步驟 3.2.1第一步:把通知類也使用注解配置 /** * 一個記錄日志的工具類 */ @Component("logger") public class Logger { /** * 期望:此方法在業務核心方法執行之前,就記錄日志 * 前置通知 */ public void beforePrintLog(){ System.out.println("前置通知:Logger類中的printLog方法開始記錄日志了"); } } 3.2.2第二步:在通知類上使用@Aspect注解宣告為切面 /** * 一個記錄日志的工具類 */ @Component("logger") @Aspect//表明當前類是一個切面類 public class Logger { /** * 期望:此方法在業務核心方法執行之前,就記錄日志 * 前置通知 */ public void beforePrintLog(){ System.out.println("前置通知:Logger類中的printLog方法開始記錄日志了"); } } 3.2.3第三步:在增強的方法上使用@Before注解配置前置通知 /** * 期望:此方法在業務核心方法執行之前,就記錄日志 * 前置通知 */ @Before("execution(* com.baidu.service.impl.*.*(..))")//表示前置通知 public void beforePrintLog(){ System.out.println("前置通知:Logger類中的printLog方法開始記錄日志了"); } 3.2.4第四步:在spring組態檔中開啟spring對注解AOP的支持 <!-- 開啟spring對注解AOP的支持 --> <aop:aspectj-autoproxy/> 3.3常用注解 3.3.1@Aspect: 作用: 把當前類宣告為切面類, 3.3.2@Before: 作用: 把當前方法看成是前置通知, 屬性: value:用于指定切入點運算式,還可以指定切入點運算式的參考, 3.3.3@AfterReturning 作用: 把當前方法看成是后置通知, 屬性: value:用于指定切入點運算式,還可以指定切入點運算式的參考, 3.3.4@AfterThrowing 作用: 把當前方法看成是例外通知, 屬性: value:用于指定切入點運算式,還可以指定切入點運算式的參考, 3.3.5@After 作用: 把當前方法看成是最終通知, 屬性: value:用于指定切入點運算式,還可以指定切入點運算式的參考, 3.3.6@Around 作用: 把當前方法看成是環繞通知, 屬性: value:用于指定切入點運算式,還可以指定切入點運算式的參考, 3.3.7@Pointcut 作用: 指定切入點運算式 屬性: value:指定運算式的內容 3.4不使用XML的配置方式 @Configuration @ComponentScan(basePackages="com.baidu") @EnableAspectJAutoProxy public class SpringConfiguration { } 總結 : 方法稱為連接點. 切入點 : 具體對那個方法做增強.切入點的運算式. Advice(通知/增強) : 對save方法做增強,撰寫程式增強要做的事情. 通知型別 : 前置通知,后置通知,最終通知,例外通知和環繞通知. Target(目標物件) : UserServiceImpl物件 Proxy (代理) : 生成的代理物件 Aspect(切面) : 抽象的概念 切面 = 切入點 + 通知. Weaving(織入) : 是指把增強應用到目標物件來創建新的代理物件的程序. AOP組態檔開發步驟 : 1 : 匯入jar包 2 : 撰寫切面類,撰寫通知的方法(自己來撰寫的) 3 : 配置切面類,進行IOC的管理 4 : 撰寫AOP的增強. <aop:config> <!--配置切面 = 切入點(運算式) + 通知 --> <aop:aspect ref="myXmlAspect"> <!--選擇通知的型別,前置通知--> <aop:before method="log" pointcut="execution(public void com.baidu.demo1.UserServiceImpl.save())"/> </aop:aspect> </aop:config> <!-- 開啟注解掃描 --> <context:component-scan base-package="com.baidu.demo1"/> <!-- 開啟注解AOP --> <aop:aspectj-autoproxy/> package com.baidu.demo1; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; /** * 注解方式的切面類 * 宣告當前類是切面類 * @author Administrator */ @Component("myAnnoAspect") @Aspect // 宣告當前類是切面類 = 切入點運算式 + 通知型別 public class MyAnnoAspect { /** * 通知方法 * 配置通知型別,注解的屬性撰寫的是切入點的運算式 * 通知型別全部采用注解方式 * @Before 前置通知 * @AfterReturning 后置通知,目標物件方法執行成功 * @AfterThrowing 例外通知 * @After 最終通知 */ // @Before(value="https://www.cnblogs.com/haizai/p/execution(public * com.baidu.demo1.*.save(..))") // @AfterReturning(value="https://www.cnblogs.com/haizai/p/execution(public * com.baidu.demo1.*.save(..))") // @AfterThrowing(value="https://www.cnblogs.com/haizai/p/execution(public * com.baidu.demo1.*.save(..))") // @After(value="https://www.cnblogs.com/haizai/p/execution(public * com.baidu.demo1.*.save(..))") public void log(){ System.out.println("記錄日志..."); } /** * 環繞通知 */ // @Around(value="https://www.cnblogs.com/haizai/p/execution(public * com.baidu.demo1.*.save(..))") @Around(value="https://www.cnblogs.com/haizai/p/MyAnnoAspect.fn()") public void arond(ProceedingJoinPoint pp){ try { System.out.println("記錄日志..."); // 讓目標物件方法執行 pp.proceed(); System.out.println("記錄日志..."); } catch (Throwable e) { e.printStackTrace(); } } /** * 定義切入點的運算式 */ @Pointcut(value="execution(public * com.baidu.demo1.*.save(..))") public void fn(){} } 基于子類的動態代理: package com.baidu.demo2; import java.lang.reflect.Method; import org.springframework.cglib.proxy.Enhancer; import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodProxy; /** * 使用cglib方式生成代理物件 * 不用實作介面 * @author Administrator */ public class MyCglibProxy { /** * 獲取到代理物件 * @return */ public static RoleServiceImpl getProxy(final RoleServiceImpl role){ Enhancer e = new Enhancer(); // 設定父類 e.setSuperclass(role.getClass()); // 設定回呼函式 e.setCallback(new MethodInterceptor() { // 呼叫代理物件的方法,那么intercept方法就會執行 public Object intercept(Object arg0, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { // 讓目標物件方法執行 Object invoke = methodProxy.invoke(role, args); // 增強 System.out.println("記錄日志..."); return invoke; } }); // 創建代理物件 Object proxy = e.create(); // 把代理物件回傳 return (RoleServiceImpl) proxy; } } <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 管理service --> <bean id="userService" class="com.baidu.demo1.UserServiceImpl"/> <!-- 先配置切面類 --> <bean id="myXmlAspect" class="com.baidu.demo1.MyXmlAspect"/> <!-- 配置AOP的增強 <aop:config> --> <!-- 配置切面 = 切入點 (運算式)+ 通知 <aop:aspect ref="myXmlAspect"> --> <!-- 選擇通知的型別,前置通知 <aop:before method="log" pointcut="execution(public void com.baidu.demo1.UserServiceImpl.save())"/> </aop:aspect> </aop:config> --> <!-- 配置AOP的增強 --> <aop:config> <!-- 撰寫切入點的運算式 --> <aop:pointcut expression="execution(public void com.baidu.demo1.UserServiceImpl.save())" id="pt"/> <aop:aspect ref="myXmlAspect"> <aop:before method="log" pointcut-ref="pt"/> </aop:aspect> </aop:config> <!-- 切入點的運算式: execution() 固定寫法 public 可以省略不寫 void 方法的回傳值,可以寫 * 號 包結構 也可以 * 號,不能省略不寫 UserServiceImpl 類,可以撰寫 * 號,常見的撰寫的寫法:*ServiceImpl 方法 可以撰寫*號 save* saveUser saveDept 引數串列 撰寫.. 指的可變引數 需求:對專案中的service的save方法進行增強 execution(public * com.baidu.*.*ServiceImpl.3save*(..)) --> <aop:config> <!-- <aop:pointcut expression="execution(public void com.baidu.demo1.UserServiceImpl.update())" id="pt"/> --> <!-- <aop:pointcut expression="execution(void com.baidu.demo1.UserServiceImpl.save())" id="pt"/> --> <!-- <aop:pointcut expression="execution(* com.baidu.demo1.UserServiceImpl.save())" id="pt"/> --> <!-- <aop:pointcut expression="execution(* com.baidu.*.UserServiceImpl.save())" id="pt"/> --> <!-- <aop:pointcut expression="execution(* com.baidu.*.*ServiceImpl.save())" id="pt"/> --> <!-- <aop:pointcut expression="execution(* com.baidu.*.*ServiceImpl.save*())" id="pt"/> --> <aop:pointcut expression="execution(* com.baidu.*.*ServiceImpl.save*(..))" id="pt"/> <aop:aspect ref="myXmlAspect"> <aop:before method="log" pointcut-ref="pt"/> </aop:aspect> </aop:config> <!-- 配置AOP的增強 --> <aop:config> <!-- 撰寫切入點的運算式 --> <aop:pointcut expression="execution(public void com.baidu.demo1.UserServiceImpl.save())" id="pt"/> <aop:aspect ref="myXmlAspect"> <!-- 前置通知 <aop:before method="log" pointcut-ref="pt"/> --> <!-- 后置通知:目標物件方法執行成功后,通知方法才執行 <aop:after-returning method="log" pointcut-ref="pt"/> --> <!-- 例外通知:目標物件方法出現例外后,通知執行 <aop:after-throwing method="log" pointcut-ref="pt"/> --> <!-- 最終通知:目標物件方法執行成功或失敗,都會執行 <aop:after method="log" pointcut-ref="pt"/> --> <!-- 環繞通知:在目標物件方法執行前后去增強,問題,默認捕獲目標物件的方法,手動讓目標物件的方法執行 --> <aop:around method="around" pointcut-ref="pt"/> </aop:aspect> </aop:config> </beans> import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(value=SpringJUnit4ClassRunner.class) // @ContextConfiguration(value="https://www.cnblogs.com/haizai/p/classpath:applicationContext.xml") //入門案例 // @ContextConfiguration(value="https://www.cnblogs.com/haizai/p/classpath:applicationContext2.xml") // 切入點的運算式 @ContextConfiguration(value="https://www.cnblogs.com/haizai/p/classpath:applicationContext3.xml") // 通知型別 public class Demo1 { @Resource(name="userService") private UserService userService; /** * AOP的入門程式 */ @Test public void run1(){ userService.save(); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/3075.html
標籤:面向對象
上一篇:uml統一建模語言學習筆記(一)
