Spring全解-07-AOP
AOP
什麼是AOP
AOP(Aspect Oriented Programming)意為:面向切面程式設計,通過預編譯方式和執行期動態代理實現程式功能的統一維護的一種技術。AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中一個重要內容,是函數語言程式設計的一種衍生範型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式可用性,同時提高了開發的效率。
AOP在Spring中的作用
提供宣告式事務,允許使用者自定義切面。
相關的專業名詞
橫切關注點:跨越應用程式多個模組的方法或功能。即,與我們業務邏輯無關的,但是我們需要關注的部分,就是橫切關注點。如日誌、安全、快取、事務等等......
切面(Aspect):橫切關注點被模組化的特殊物件。即,它是一個類。
通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
目標(Target):被通知物件。
代理(Proxy):向目標物件應用通知之後建立的物件。
切入點(PointCut):切面通知執行的“地點”的定義。
連線點(JointPoint):與切入點匹配的執行點。
SpringAOP中,通過Advice定義橫切邏輯,Spring中支援5種類型的Advice。
前置通知
後置通知
環繞通知
異常丟擲通知
引介通知
使用AOP時,需要匯入一個AOP織入
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency>
三種方式實現AOP
第一種:通過Spring API實現
先定義介面和介面的實現類
public interface UserService { public void add(); public void delete();public void update(); public void search(); } public class UserServiceImpl implements UserService{ @Override public void add() { System.out.println("增加使用者"); } @Override public void delete() { System.out.println("刪除使用者"); } @Override public void update() { System.out.println("更新使用者"); } @Override public void search() { System.out.println("查詢使用者"); } }
然後寫增強類,一個前置增強MethodBeforeAdvice,一個後置增強AfterReturningAdvice
public class Log implements MethodBeforeAdvice { //method : 要執行的目標物件的方法 //objects : 被呼叫的方法的引數 //Object : 目標物件 @Override public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被執行了"); } } public class AfterLog implements AfterReturningAdvice { //returnValue 返回值 //method被呼叫的方法 //args 被呼叫的方法的物件的引數 //target 被呼叫的目標物件 @Override public void afterReturning(Object returnValue, Method method, Object[] args,Object target) throws Throwable { System.out.println("執行了" + target.getClass().getName() +"的"+method.getName()+"方法," +"返回值:"+returnValue); } }
最後去spring的檔案中註冊 , 並實現aop切入實現 , 注意匯入約束
<?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"> <!--註冊bean--> <bean id="userService" class="com.li.service.UserServiceImpl"/> <bean id="log" class="com.li.log.Log"/> <bean id="afterLog" class="com.li.log.AfterLog"/> <!--aop的配置--> <aop:config> <!--切入點 expression:表示式匹配要執行的方法--> <aop:pointcut id="pointcut" expression="execution(* com.li.service.UserServiceImpl.*(..))"/> <!--執行環繞; advice-ref執行方法 . pointcut-ref切入點--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config> </beans>
public class MyTest { @Test public void test(){ ApplicationContext context = newClassPathXmlApplicationContext("beans.xml"); UserService userService = (UserService) context.getBean("userService"); userService.search(); } }
Spring的Aop就是將公共的業務 (日誌 , 安全等) 和領域業務結合起來 , 當執行領域業務時 , 將會把公共業務加進來 . 實現公共業務的重複利用 . 領域業務更純粹 , 程式猿專注領域業務 , 其本質還是動態代理。
第二種方式:自定義類來實現AOP
寫我們自己的一個切入類,自定義切面
public class DiyPointcut { public void before(){ System.out.println("---------方法執行前---------"); } public void after(){ System.out.println("---------方法執行後---------"); } }
<!--第二種方式自定義實現--> <!--註冊bean--> <bean id="diy" class="com.li.config.DiyPointcut"/> <!--aop的配置--> <aop:config> <!--第二種方式:使用AOP的標籤實現--> <aop:aspect ref="diy"> <aop:pointcut id="diyPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/> <aop:before pointcut-ref="diyPonitcut" method="before"/> <aop:after pointcut-ref="diyPonitcut" method="after"/> </aop:aspect> </aop:config>
public class MyTest { @Test public void test(){ ApplicationContext context = newClassPathXmlApplicationContext("beans.xml"); UserService userService = (UserService) context.getBean("userService"); userService.add(); } }
第三種方式:使用註解實現
編寫一個註解實現的增強類
package com.li.config; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class AnnotationPointcut { @Before("execution(* com.li.service.UserServiceImpl.*(..))") public void before(){ System.out.println("---------方法執行前---------"); } @After("execution(* com.li.service.UserServiceImpl.*(..))") public void after(){ System.out.println("---------方法執行後---------"); } @Around("execution(* com.li.service.UserServiceImpl.*(..))") public void around(ProceedingJoinPoint jp) throws Throwable { System.out.println("環繞前"); System.out.println("簽名:"+jp.getSignature()); //執行目標方法proceed Object proceed = jp.proceed(); System.out.println("環繞後"); System.out.println(proceed); } }
注意:使用註解實現,需要在Spring配置檔案中,註冊bean和增加註解驅動的配置
<!--第三種方式:註解實現--> <bean id="annotationPointcut" class="com.li.config.AnnotationPointcut"/> <aop:aspectj-autoproxy/>
aop:aspectj-autoproxy:說明
通過aop名稱空間的<aop:aspectj-autoproxy />宣告自動為spring容器中那些配置@aspectJ切面的bean建立代理,織入切面。當然,spring 在內部依舊採用AnnotationAwareAspectJAutoProxyCreator進行自動代理的建立工作,但具體實現的細節已經被<aop:aspectj-autoproxy />隱藏起來了
<aop:aspectj-autoproxy />有一個proxy-target-class屬性,預設為false,表示使用jdk動態代理織入增強,當配為<aop:aspectj-autoproxy poxy-target-class="true"/>時,表示使用CGLib動態代理技術織入增強。不過即使proxy-target-class設定為false,如果目標類沒有宣告介面,則spring將自動使用CGLib動態代理。