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);
    }
}
相关推荐
SimonKing27 分钟前
Archery:开源、一站式的数据库 SQL 审核与运维平台
java·后端·程序员
DemonAvenger1 小时前
MySQL海量数据快速导入导出技巧:从实战到优化
数据库·mysql·性能优化
皮皮林55112 小时前
IDEA 源码阅读利器,你居然还不会?
java·intellij idea
程序新视界13 小时前
MySQL中什么是回表查询,如何避免和优化?
mysql
卡尔特斯16 小时前
Android Kotlin 项目代理配置【详细步骤(可选)】
android·java·kotlin
白鲸开源16 小时前
Ubuntu 22 下 DolphinScheduler 3.x 伪集群部署实录
java·ubuntu·开源
ytadpole16 小时前
Java 25 新特性 更简洁、更高效、更现代
java·后端
纪莫17 小时前
A公司一面:类加载的过程是怎么样的? 双亲委派的优点和缺点? 产生fullGC的情况有哪些? spring的动态代理有哪些?区别是什么? 如何排查CPU使用率过高?
java·java面试⑧股
JavaGuide17 小时前
JDK 25(长期支持版) 发布,新特性解读!
java·后端
用户37215742613517 小时前
Java 轻松批量替换 Word 文档文字内容
java