2023-01-18
一、Spring中的AOP
1、AspectJ
(1)簡介
Java社區里最完整最流行的AOP框架
在Spring2.0以上版本中,可以使用AspectJ注解或基于XML配置的AOP
(2)使用AspectJ步驟
①在spring核心包的基礎上添加支持jar包
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>5.3.10</version> </dependency>
②創建spring的組態檔,配置自動掃描的包和AspectJ注解支持
<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 開啟組件掃描--> <context:component-scan base-package="com.hh"></context:component-scan> <!-- 開啟AspectJ注解支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
③創建一個類作為切面,在類的上面添加注解
@Component:將當前類標識為一個組件
@Aspect:將當前類標識為切面類(非核心業務提取類)
@Component @Aspect public class MyLogging { /** * 方法之前 */ public static void beforeMethod(String methodName,Object[] arg){ System.out.println("==>Calc中"+methodName+"方法(),引數:"+ Arrays.toString(arg)); } /** * 方法之后 */ public static void afterMethod(String methodName,Object rs){ System.out.println("==>Calc中"+methodName+"方法(),結果:"+rs); } }
④將日志類中的方法添加“通知注解”
@Before
⑤測驗
二、Spring中AOP概述
1、AOP:Aspect-Oriented Programming,面向切面編程(面向物件的一種補充)
優勢:解決了代碼分散與代碼混亂的問題
2、OOP:Object-Oriented Programming,面向物件編程
三、Spring中AOP相關術語
1、橫切關注點
非核心業務代碼
2、切面(Aspect)
將橫切關注點提取到類中,這個類稱之為切面類
3、通知(Advice)
將橫切關注點提取到類之后,橫切關注點更名為通知
4、目標(Target)
目標物件,指的是需要被代理的物件(實作類CalcImpl)
5、代理(Proxy)
代理物件可以理解為中介
6、連接點(Joinpoint)
通知方法需要指定通知位置,這個位置稱之為連接點(通知之前)
7、切入點(pointcut)
通知方法需要指定通知位置,這個位置稱之為切入點(通知之后)
四、AspectJ中切入點運算式
1、語法:@Before(value="https://www.cnblogs.com/isDaHua/archive/2023/01/18/execution(權限修飾符 回傳值型別 包名.類名.方法名(引數型別))")
2、通配符
(1)【*】
*:可以代表任意權限修飾符&回傳值型別
*:可以代表任意包名、任意類名、任意方法名
(2)【..】
.. 代表任意引數型別及引數個數
3、重用切入點運算式
(1)使用@PointCut注解,提取可重用的切入點運算式
//重用切入點運算式 @Pointcut("execution(* com.hh.aop.CalcImpl.*(..))") public void myPointCut(){}
(2)使用方法名()引入切入點運算式
@Before(value = "https://www.cnblogs.com/isDaHua/archive/2023/01/18/myPointCut()")
@After("myPointCut()")
五、AspectJ中JoinPoint物件
1、JoinPoint:切入點物件
2、作用:
(1)獲取方法名稱
String methodName = joinPoint.getSignature().getName();
注:joinPoint.getSignature() 表示方法簽名(方法簽名=方法名+引數串列)
(2)獲取引數
Object[] args = joinPoint.getArgs();
六、AspectJ中通知
1、前置通知
(1)語法:@Before
(2)執行時機:指定方法之前執行
指定方法:即切入點運算式設定位置
注:如果目標方法中有例外,會執行
(3)示例代碼
@Before(value = "https://www.cnblogs.com/isDaHua/archive/2023/01/18/myPointCut()") public void beforeMethod(JoinPoint joinPoint){ //獲取方法名稱 String methodName = joinPoint.getSignature().getName(); //獲取引數 Object[] args = joinPoint.getArgs(); System.out.println("【前置通知】==>Calc中"+methodName+"方法(),引數:"+ Arrays.toString(args)); }
2、后置通知
(1)語法:@After
(2)執行時機:指定方法所有通知執行之后執行
注:如果目標方法中有例外,會執行
(3)示例代碼
@After("myPointCut()")
public void afterMethod(JoinPoint joinPoint){
//獲取方法名稱
String methodName = joinPoint.getSignature().getName();
//獲取引數
Object[] args = joinPoint.getArgs();
System.out.println("【后置通知】==>Calc中"+methodName+"方法(),之后執行:"+ Arrays.toString(args));
}
3、回傳通知
(1)語法:@AfterReturnning
(2)執行時機:指定方法回傳結果時執行
注:@AfterReturnning中returning屬性中的的屬性名與入參中引數名一致;
如果目標方法中有例外,不執行
(3)示例代碼
@AfterReturning(value = "https://www.cnblogs.com/isDaHua/archive/2023/01/18/myPointCut()",returning = "rs") public void afterReturnning(JoinPoint joinPoint,Object rs){ //獲取方法名稱 String methodName = joinPoint.getSignature().getName(); //獲取引數 Object[] args = joinPoint.getArgs(); System.out.println("【回傳通知】==>Calc中"+methodName+"方法(),回傳結果執行!結果:"+ rs); }
4、例外通知
(1)語法:@AfterThrowing
(2)執行時機::指定方法出現例外時執行
注:@AfterThrowing中throwing屬性中的的屬性名與入參中引數名一致;
如果目標方法中有例外,執行
(3)示例代碼
@AfterThrowing(value = "https://www.cnblogs.com/isDaHua/archive/2023/01/18/myPointCut()",throwing = "ex") public void afterThrowing(JoinPoint joinPoint,Exception ex){ //獲取方法名稱 String methodName = joinPoint.getSignature().getName(); //獲取引數 Object[] args = joinPoint.getArgs(); System.out.println("【例外通知】==>Calc中"+methodName+"方法(),出現例外時執行!例外:"+ ex); }
說明:
①有例外:前置通知—>例外通知—>后置通知
②無例外:前置通知—>回傳通知—>后置通知
5、環繞通知(前四個通知整合)
(1)語法:@Around
(2)作用:整合前四個通知
(3)注意:引數中必須使用ProceedingJoinPoint
(4)示例代碼
@Around(value = "https://www.cnblogs.com/isDaHua/archive/2023/01/18/myPointCut()") public Object aroundMethod(ProceedingJoinPoint pjp){ //獲取方法名稱 String methodName = pjp.getSignature().getName(); //獲取引數 Object[] args = pjp.getArgs(); //定義回傳值 Object rs = null; try { //前置通知 ) System.out.println("【前置通知】==>Calc中"+methodName+"方法(),引數:"+ Arrays.toString(args)); //觸發目標物件的目標方法(加減乘除) rs = pjp.proceed(); //回傳通知(有例外不執行 System.out.println("【回傳通知】==>Calc中"+methodName+"方法(),回傳結果執行!結果:"+ rs); } catch (Throwable throwable) { throwable.printStackTrace(); //例外通知 System.out.println("【例外通知】==>Calc中"+methodName+"方法(),出現例外時執行!例外:"+ throwable); }finally { //后置通知(有例外執行) System.out.println("【后置通知】==>Calc中"+methodName+"方法(),之后執行:"+ Arrays.toString(args)); } return rs; }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/542197.html
標籤:其他
上一篇:字符編碼
