Spring入門學習---06
阿新 • • 發佈:2021-11-04
Spring
AOP
1、什麼是AOP?
在軟體業,AOP為Aspect Oriented Programming的縮寫,意為:面向切面程式設計,通過預編譯方式和執行期間動態代理實現程式功能的統一維護的一種技術。AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函數語言程式設計的一種衍生範型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率。 ------- 源自百度百科
2、AOP在Spring中的作用
1. 提供了宣告式事務
2. 允許使用者自定義切面(面向切面程式設計)
在面向切面程式設計時,可能會遇到以下的術語:
-
-
切面(ASPECT):橫切關注點 被模組化 的特殊物件。即,它是一個類。
-
通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
-
目標(Target):代理的目標物件。
-
代理(Proxy):生成的代理物件。
-
切入點(PointCut):指要對哪些連線點進行攔截。
-
在Advice中提供了五個方法供我們使用
通知型別 | 連線點 | 實現介面 |
前置通知 | 方法前 | org.springfraamework.aop.MethodBeforeAdvice |
後置通知 | 方法後 | org.springfraamework.aop.AfterReturningAdvice |
環繞通知 | 方法前後 | org.aopalliance.intercept.MethodInterceptor |
異常丟擲通知 | 方法丟擲異常 | org.springfraamework.aop.ThrowsAdvice |
引介通知 | 類中增加新的方法屬性 | org.springfraamework.aop.IntroductionInterceptor |
3、AOP的使用
使用前先匯入它的依賴和約束
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version> </dependency> ======================================== <?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 https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
1. 使用AOP的介面
1、編寫一個UserService的介面
public interface UserService { public void add(); public void delete(); public void update(); public void select(); }
2、編寫一個UserServiceImpl
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 select() { System.out.println("查詢了一個使用者"); } }
3、編寫兩個切入點並繼承AOP的介面
前置通知
public class Log implements MethodBeforeAdvice { @Override public void before(Method method, Object[] objects, Object o) throws Throwable { assert o != null; System.out.println(o.getClass().getName() + "的" + method.getName() + "被執行了"); } }
後置通知
public class AfterLog implements AfterReturningAdvice { @Override public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("執行了" + method.getName() + "返回結果為:" + o); } }
4、配置AOP
<!-- bean註冊 --> <bean class="com.charles.service.UserServiceImpl" id="userService"/> <bean class="com.charles.log.AfterLog" id="afterLog"/> <bean class="com.charles.log.Log" id="log"/> <!-- 方式一:使用原生的API介面 --> <!-- 配置AOP:需要匯入aop的約束 --> <aop:config> <!-- 切入點:expression:表示式(固定格式) execution(要執行的位置 *(修飾值) *(返回值) *(類名) *(方法名) *(引數)) --> <aop:pointcut id="pointcut" expression="execution(* com.charles.service.UserServiceImpl.*(..))"/> <!-- 執行環繞增強 --> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config>
2、自定義AOP
1、自定義一個AOP類
public class DiyLog { public void before(){ System.out.println("===========前置通知==========="); } public void after(){ System.out.println("===========後置通知==========="); } }
2、配置AOP
<bean class="com.charles.log.DiyLog" id="diyLog"/> <!-- 方拾二:自定義類 --> <aop:config> <aop:aspect ref="diyLog"> <aop:pointcut id="pointcut" expression="execution(* com.charles.service.UserServiceImpl.*(..))"/> <aop:before method="before" pointcut-ref="pointcut"/> <aop:after method="after" pointcut-ref="pointcut"/> </aop:aspect> </aop:config>
3、進行測試
3、利用註釋
1、編寫AOP類
@Aspect public class AnnovationLog { @Before("execution(* com.charles.service.UserServiceImpl.*(..))") public void beforeLog(){ System.out.println("===========前置通知==========="); } @After("execution(* com.charles.service.UserServiceImpl.*(..))") public void afterLog(){ System.out.println("===========後置通知==========="); } @Around("execution(* com.charles.service.UserServiceImpl.*(..))") public void around(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("環繞前"); Signature signature = joinPoint.getSignature(); System.out.println(signature); joinPoint.proceed(); System.out.println("環繞後"); } }
2、配置AOP,開啟註釋
<!-- 開啟註解 --> <aop:aspectj-autoproxy/> <bean class="com.charles.log.AnnovationLog" id="annovationLog"/>