網(wǎng)站首頁 編程語言 正文
目錄
1.準(zhǔn)備工作
2.配置Spring配置文件
1.準(zhǔn)備工作
添加依賴
準(zhǔn)備代碼
和基于注解的AOP準(zhǔn)備工作一樣
2.配置Spring配置文件
<!-- 配置目標(biāo)類的bean -->
<bean id="calculatorPure" class="com.atguigu.aop.imp.CalculatorPureImpl"/>
<!-- 配置切面類的bean -->
<bean id="logAspect" class="com.atguigu.aop.aspect.LogAspect"/>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切入點(diǎn)表達(dá)式 -->
<aop:pointcut id="logPointCut" expression="execution(* *..*.*(..))"/>
<!-- aop:aspect標(biāo)簽:配置切面 -->
<!-- ref屬性:關(guān)聯(lián)切面類的bean -->
<aop:aspect ref="logAspect">
<!-- aop:before標(biāo)簽:配置前置通知 -->
<!-- method屬性:指定前置通知的方法名 -->
<!-- pointcut-ref屬性:引用切入點(diǎn)表達(dá)式 -->
<aop:before method="printLogBeforeCore" pointcut-ref="logPointCut"/>
<!-- aop:after-returning標(biāo)簽:配置返回通知 -->
<!-- returning屬性:指定通知方法中用來接收目標(biāo)方法返回值的參數(shù)名 -->
<aop:after-returning
method="printLogAfterCoreSuccess"
pointcut-ref="logPointCut"
returning="targetMethodReturnValue"/>
<!-- aop:after-throwing標(biāo)簽:配置異常通知 -->
<!-- throwing屬性:指定通知方法中用來接收目標(biāo)方法拋出異常的異常對象的參數(shù)名 -->
<aop:after-throwing
method="printLogAfterCoreException"
pointcut-ref="logPointCut"
throwing="targetMethodException"/>
<!-- aop:after標(biāo)簽:配置后置通知 -->
<aop:after method="printLogCoreFinallyEnd" pointcut-ref="logPointCut"/>
<!-- aop:around標(biāo)簽:配置環(huán)繞通知 -->
<!--<aop:around method="……" pointcut-ref="logPointCut"/>-->
</aop:aspect>
</aop:config>
對比基于注解實(shí)現(xiàn)的
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 {
//統(tǒng)一切點(diǎn)管理
//1.當(dāng)前類中提取
@Pointcut(value = "execution(* com.atguigu..impl.*.*(..))" )
public void pc(){}
//2.創(chuàng)建一個存儲切點(diǎn)的類
// @Before(value = "execution(* com.atguigu..impl.*.*(..))")
@Before(value = "pc()")
public void before(JoinPoint joinPoint){
//1.獲取目標(biāo)對象對應(yīng)的類的信息
String simpleName = joinPoint.getTarget().getClass().getSimpleName();
System.out.println("獲取目標(biāo)對象對應(yīng)的類的信息:"+simpleName);
//2.獲取方法的名稱
String name = joinPoint.getSignature().getName();
System.out.println("獲取方法的名稱:"+name);
//3.獲取方法的返回修飾符
int modifiers = joinPoint.getSignature().getModifiers();
String string = Modifier.toString(modifiers);//利用反射將數(shù)字轉(zhuǎn)換為字符串
System.out.println("獲取方法的返回修飾符:"+string);
//4.獲取參數(shù)列表
Object[] args = joinPoint.getArgs();
System.out.println("獲取參數(shù)列表:"+ Arrays.toString(args));
}
@AfterReturning(value = "com.atguigu.pointcut.MyPointCut.myPc()",returning = "result")
public void afterReturning(Object result){
//5.獲取返回結(jié)果
System.out.println("獲取返回結(jié)果:"+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
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-05-14 react中的axios模塊你了解嗎_React
- 2023-04-07 React替換傳統(tǒng)拷貝方法的Immutable使用_React
- 2023-10-10 css像素的問題 物理像素 獨(dú)立像素 設(shè)備像素比
- 2023-01-11 Pytorch如何把Tensor轉(zhuǎn)化成圖像可視化_python
- 2022-05-04 R語言數(shù)據(jù)類型與相應(yīng)運(yùn)算的實(shí)現(xiàn)_R語言
- 2022-12-11 SQL中創(chuàng)建存儲過程_MsSql
- 2022-06-02 TensorFlow實(shí)現(xiàn)簡單線性回歸_python
- 2022-11-14 C#中對集合排序的三種方式_C#教程
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支