網站首頁 編程語言 正文
AOP切面編程
- 通知類型
- 表達式
- 重用表達式
- 切面優先級
- 使用注解開發,加上注解實現某些功能
簡介
- 動態代理分為JDK動態代理和cglib動態代理
- 當目標類有接口的情況使用JDK動態代理和cglib動態代理,沒有接口時只能使用cglib動態代理
- JDK動態代理動態生成的代理類會在com.sun.proxy包下,類名為$proxy1,和目標類實現相同的接口
- cglib動態代理動態生成的代理類會和目標在在相同的包下,會繼承目標類
- 動態代理(InvocationHandler):JDK原生的實現方式,需要被代理的目標類必須實現接口。因為這個技術要求代理對象和目標對象實現同樣的接口(兄弟兩個拜把子模式)。
- cglib:通過繼承被代理的目標類(認干爹模式)實現代理,所以不需要目標類實現接口。
- AspectJ:是AOP思想的一種實現。本質上是靜態代理,將代理邏輯“織入”被代理的目標類編譯得到的字節碼文件,所以最終效果是動態的。weaver就是織入器。Spring只是借用了AspectJ中的注解。
依賴
<!--spring context依賴-->
<!--當你引入Spring Context依賴之后,表示將Spring的基礎依賴引入了-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.2</version>
</dependency>
<!--spring aop依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>6.0.2</version>
</dependency>
<!--spring aspects依賴-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.0.2</version>
</dependency>
注解說明
-
@Aspect
表示這個類是一個切面類 -
@Component
注解保證這個切面類能夠放入IOC容器
簡單使用
幾種通知類型
- 前置通知
@Before()
- 環繞通知
@Around()
- 返回通知
@AfterReturning()
- 異常通知
@AfterThrowing()
- 后置通知
@After()
表達式介紹
切入點表達式
在方法中可以傳入參數public void afterMethod(JoinPoint joinPoint)
,類型為JoinPoint
-
獲取連接點的簽名信息
-
String methodName = joinPoint.getSignature().getName()
-
-
獲取目標方法的返回值
-
其中表達式中
returning
后面參數result
要與public void afterReturningMethod(JoinPoint joinPoint, Object result)
傳入參數名稱一樣,類型可以不一樣但是名稱要一樣。 -
@AfterReturning(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))", returning = "result") public void afterReturningMethod(JoinPoint joinPoint, Object result){ String methodName = joinPoint.getSignature().getName(); System.out.println("Logger-->返回通知,方法名:"+methodName+",結果:"+result); }
-
-
獲取目標方法的異常
-
在表達式中加入
throwing = "ex"
,和上面一樣,傳入參數名稱要一直,也要是ex
,如:Throwable ex
-
@AfterThrowing(value = "execution(* com.atguigu.aop.annotation.CalculatorImpl.*(..))", throwing = "ex") public void afterThrowingMethod(JoinPoint joinPoint, Throwable ex){ String methodName = joinPoint.getSignature().getName(); System.out.println("Logger-->異常通知,方法名:"+methodName+",異常:"+ex); }
-
切入點表達式使用
前置通知
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
// 設置切入點和通知類型
@Before(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))")
public void beforeMethod(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
System.out.println("Logger-->前置通知" + "參數:" + args[0] + "," + args[1]);
}
}
后置通知
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
@After(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))")
public void afterMethod(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();// 方法名
System.out.println("Logger-->后置通知,方法名:" + name);
}
}
返回通知
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
@AfterReturning(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))", returning = "result")
public void afterReturn(JoinPoint joinPoint, Object result) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->返回通知,方法名:" + methodName + ",結果:" + result);
}
}
異常通知
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
@AfterThrowing(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))", throwing = "ex")
public void afterThrowingMethod(JoinPoint joinPoint, Exception ex) {
String methodName = joinPoint.getSignature().getName();
System.out.println("Logger-->異常通知,方法名:" + methodName + ",異常:" + ex);
}
}
環繞通知
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
public class LogAspect {
@Around(value = "execution(public int com.example.aop.annoaop.CalculatorImpl.*(..))")
public Object aroundMethod(ProceedingJoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
Object result = null;
try {
System.out.println("環繞通知-->目標對象方法執行之前");
// 目標對象(連接點)方法的執行
result = joinPoint.proceed();
System.out.println("環繞通知-->目標對象方法返回值之后");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("環繞通知-->目標對象方法出現異常時");
} finally {
System.out.println("環繞通知-->目標對象方法執行完畢");
}
return result;
}
}
重用切入點表達式
申明表達式
@Pointcut("execution(* com.atguigu.aop.annotation.*.*(..))")
public void pointCut(){}
在方法中使用
@Before("pointCut()")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:"+methodName+",參數:"+args);
}
在不同切面中使用
@Before("com.atguigu.aop.CommonPointCut.pointCut()")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
String args = Arrays.toString(joinPoint.getArgs());
System.out.println("Logger-->前置通知,方法名:"+methodName+",參數:"+args);
}
切面的優先級
相同目標方法上同時存在多個切面時,切面的優先級控制切面的內外嵌套順序。
- 優先級高的切面:外面
- 優先級低的切面:里面
使用@Order注解可以控制切面的優先級:
- @Order(較小的數):優先級高
- @Order(較大的數):優先級低
將@Order注解放在切面類上而不是方法上!!!
- 優先級低的
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
@Order(4)
public class LogAspect {
@After(value = "pointcut()")
public void afterMethod2(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();// 方法名
System.out.println("Logger-->后置通知 111111,方法名:" + name);
}
}
- 優先級高的
@Aspect// 表示這個類是一個切面類
@Component// 注解保證這個切面類能夠放入IOC容器
@Order(1)
public class NewLogAspect {
@After(value = "com.example.aop.annoaop.LogAspect.pointcut()")
public void afterMethod(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();// 方法名
System.out.println("Logger-->后置通知 444444,方法名:" + name);
}
}
效果
原先交換順序后
沒有交換順序前
注解使用
使用AOP在方法或者接口上寫上某些注解,完成特定方法。
實現思路
- 創建
@interface
- 和上面一樣要寫
Aspect
,并且要被spring
管理 - 在方法或者接口上加上
@interface
注解
實現示例
- 創建
@interface
并命名為BunnyLog
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
// 加上這個注解打印當前時間
public @interface BunnyLog {
}
-
使用AOP切面,需要注意的是:
-
如果只是單純的加上注解,不考慮指定類或者類中方法,完成某些功能,要修改下面切入點
-
@Pointcut(value = "@annotation(com.example.aop.annoaop.BunnyLog)") public void bunnyPointcut() { }
-
-
如果想指定某些類或者某些方法下的
-
@Pointcut("execution(* com.example.aop.annoaop.*.*(..)) && @annotation(com.example.aop.annoaop.BunnyLog)") public void bunnyPointcut() { }
-
-
-
創建切面
@Aspect
@Component
public class BunnyAspect {
/**
* 切入點,并且加上了 @BunnyLog注解
*/
@Pointcut(value = "execution(* com.example.aop.annoaop.*.*(..)) || @annotation(com.example.aop.annoaop.BunnyLog)")
public void bunnyPointcut() {
}
@Before("bunnyPointcut()")
public void before(JoinPoint joinPoint) {
String name = joinPoint.getSignature().getName();
System.out.println("------AOP前置通知生效,方法名稱------>" + name);
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyy年MM月dd日 HH:mm:ss");
String format = localDateTime.format(timeFormatter);
System.out.println("BunnyAspect,現在時間====>" + format);
}
}
- 普通方法,需要在類上加上
@Component
被spring管理,之后再方法中加上注解@BunnyLog
@Component
public class BunnyTestImpl {
@BunnyLog
public void method() {
LocalDateTime localDateTime = LocalDateTime.now();
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyy年MM月dd日 HH:mm:ss");
String format = localDateTime.format(timeFormatter);
System.out.println("測試方法,現在時間====>" + format);
}
}
- 創建一個測試類
public class TestBefore {
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BunnyTestImpl bean = context.getBean(BunnyTestImpl.class);
bean.method();
}
}
執行結果:
r);
System.out.println(“測試方法,現在時間====>” + format);
}
}
- 創建一個測試類
```java
public class TestBefore {
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
BunnyTestImpl bean = context.getBean(BunnyTestImpl.class);
bean.method();
}
}
執行結果:
原文鏈接:https://blog.csdn.net/weixin_46533577/article/details/136605066
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-07-04 聯邦學習FedAvg中模型聚合過程的理解分析_其它綜合
- 2022-04-14 Python中創建表格詳細過程_python
- 2022-08-28 redis 主從同步部署
- 2022-09-25 python mac版本解釋器安裝
- 2022-10-30 Android中二維碼的掃描和生成(使用zxing庫)_Android
- 2023-01-07 詳解C++11中綁定器bind的原理與使用_C 語言
- 2022-09-15 Python淺析生成器generator的使用_python
- 2022-12-10 C++中的結構體vector排序問題_C 語言
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支