網站首頁 編程語言 正文
目錄
1.準備工作
2.配置Spring配置文件
1.準備工作
添加依賴
準備代碼
和基于注解的AOP準備工作一樣
2.配置Spring配置文件
<!-- 配置目標類的bean -->
<bean id="calculatorPure" class="com.atguigu.aop.imp.CalculatorPureImpl"/>
<!-- 配置切面類的bean -->
<bean id="logAspect" class="com.atguigu.aop.aspect.LogAspect"/>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切入點表達式 -->
<aop:pointcut id="logPointCut" expression="execution(* *..*.*(..))"/>
<!-- aop:aspect標簽:配置切面 -->
<!-- ref屬性:關聯切面類的bean -->
<aop:aspect ref="logAspect">
<!-- aop:before標簽:配置前置通知 -->
<!-- method屬性:指定前置通知的方法名 -->
<!-- pointcut-ref屬性:引用切入點表達式 -->
<aop:before method="printLogBeforeCore" pointcut-ref="logPointCut"/>
<!-- aop:after-returning標簽:配置返回通知 -->
<!-- returning屬性:指定通知方法中用來接收目標方法返回值的參數名 -->
<aop:after-returning
method="printLogAfterCoreSuccess"
pointcut-ref="logPointCut"
returning="targetMethodReturnValue"/>
<!-- aop:after-throwing標簽:配置異常通知 -->
<!-- throwing屬性:指定通知方法中用來接收目標方法拋出異常的異常對象的參數名 -->
<aop:after-throwing
method="printLogAfterCoreException"
pointcut-ref="logPointCut"
throwing="targetMethodException"/>
<!-- aop:after標簽:配置后置通知 -->
<aop:after method="printLogCoreFinallyEnd" pointcut-ref="logPointCut"/>
<!-- aop:around標簽:配置環繞通知 -->
<!--<aop:around method="……" pointcut-ref="logPointCut"/>-->
</aop:aspect>
</aop:config>
對比基于注解實現的
package com.atguigu.advice;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.lang.reflect.Modifier;
import java.util.Arrays;
/**
* @Auther: Ying
* @Date: 2024/2/10 - 02 - 10 - 14:22
* @Description: com.atguigu.advice
* @version: 1.0
*/
@Component
@Aspect
public class MyAdvice {
//統一切點管理
//1.當前類中提取
@Pointcut(value = "execution(* com.atguigu..impl.*.*(..))" )
public void pc(){}
//2.創建一個存儲切點的類
// @Before(value = "execution(* com.atguigu..impl.*.*(..))")
@Before(value = "pc()")
public void before(JoinPoint joinPoint){
//1.獲取目標對象對應的類的信息
String simpleName = joinPoint.getTarget().getClass().getSimpleName();
System.out.println("獲取目標對象對應的類的信息:"+simpleName);
//2.獲取方法的名稱
String name = joinPoint.getSignature().getName();
System.out.println("獲取方法的名稱:"+name);
//3.獲取方法的返回修飾符
int modifiers = joinPoint.getSignature().getModifiers();
String string = Modifier.toString(modifiers);//利用反射將數字轉換為字符串
System.out.println("獲取方法的返回修飾符:"+string);
//4.獲取參數列表
Object[] args = joinPoint.getArgs();
System.out.println("獲取參數列表:"+ Arrays.toString(args));
}
@AfterReturning(value = "com.atguigu.pointcut.MyPointCut.myPc()",returning = "result")
public void afterReturning(Object result){
//5.獲取返回結果
System.out.println("獲取返回結果:"+result);
}
@After(value = "pc()")
public void after(){
}
@AfterThrowing(value = "pc()",throwing = "throwable")
public void afterThrowing(Throwable throwable){
//6.獲取異常的信息
System.out.println("獲取異常的信息:"+throwable);
}
}
原文鏈接:https://blog.csdn.net/weixin_69134926/article/details/136469697
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-10-25 Python第三方常用模塊openpyxl的簡單介紹_python
- 2023-03-28 python如何實現向上取整_python
- 2022-10-12 redis刪除指定key的實現步驟_Redis
- 2022-05-17 Spring Cloud Ribbon詳解
- 2023-01-01 MongoDB?Shell常用基本操作命令詳解_MongoDB
- 2022-04-22 nvm切換node版本后提示npm : 無法將“npm”項識別為 cmdlet、函數、腳本文件或可運
- 2023-01-07 基于Go語言實現選擇排序算法及優化_Golang
- 2022-06-15 GO文件創建及讀寫操作示例詳解_Golang
- 欄目分類
-
- 最近更新
-
- 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同步修改后的遠程分支