日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

SpringAOP基于XML方式實(shí)現(xiàn)(了解)

作者:W-琑 更新時間: 2024-03-07 編程語言

目錄

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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新