SpringAOP基于XML方式实现(了解)

目录

1.准备工作

2.配置Spring配置文件


1.准备工作

添加依赖

准备代码

和基于注解的AOP准备工作一样

2.配置Spring配置文件

XML 复制代码
<!-- 配置目标类的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>

对比基于注解实现的

java 复制代码
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);
    }
}
相关推荐
BerrySen1784 分钟前
迈向 Next-Gen Java:企业级高并发架构演进与大模型 Agent 落地深度实战
java·开发语言·架构
technology_x19 分钟前
2026年财务报表分析软件测评:兼容与安全解析
java·服务器·前端
兰令水23 分钟前
hot100【acm版】【2026.7.25/26打卡-java版本】
java·算法·排序算法
Nebula_g42 分钟前
Java实现本地Socket通信(三)
java·开发语言·学习·socket·可视化
一水1 小时前
java运行排错,新码旧jar
java·开发语言·jar
wuqingshun3141591 小时前
JAVA中的注解原理是什么?
java
Python+991 小时前
Java 编程语言入门指南
java·开发语言
huahailing10243 小时前
Spring Boot 集成 XXL-Job 完整实现方案(支持动态CRUD)
java·spring boot·后端
薛定谔的猫-菜鸟程序员3 小时前
基于 Electron 的本地短视频解析与下载工具:架构设计与工程实践
java·electron·音视频
音符犹如代码4 小时前
Arthas Profiler 火焰图实战:CPU 热点在哪一目了然
java·jvm·spring boot