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);
    }
}
相关推荐
lhldsg1 小时前
全民健身解决方案小程序开发:从0到1的技术实战
java·数据库·需求分析
jason成都1 小时前
Spring WebFlux 适配达梦新方案|dm‑r2dbc:Netty 异步传输的实验性 R2DBC 驱动
java·后端·spring
SendTomo1 小时前
【技术交流】从0到1构建一个汉字学习平台:汉字吧(hanzi8.com)的技术架构猜想
redis·mysql·nginx·php·个人开发
泡海椒1 小时前
内置SPI函数库详解:JQuick-Java Builtin工具类实战用法
java·开发语言·python
辰烨chenye1 小时前
LeetCode Hot 100 题解 · 二分篇
java·算法·leetcode
IT大白鼠1 小时前
MySQL 分布式集群系列 · 第四篇——实操部署指南:从零搭建生产级 MySQL NDB 集群
数据库·分布式·mysql
IT大白鼠2 小时前
MySQL 分布式集群系列 · 第三篇——核心原理精讲:NDB 自动分片、多主写入与数据同步
数据库·分布式·mysql
小羊没烦恼!2 小时前
Hello Web API系列教程——Web API与国际化
java·服务器·前端·javascript·php
小荷才露尖尖角,早有蜻蜓立上头3 小时前
过滤器的4种创建方式
java
大圣编蚕3 小时前
Java ByteArrayInputStream 详解:从入门到实战
java·开发语言·算法