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);
    }
}
相关推荐
ya888g4 分钟前
GESP C++四级样题卷
java·c++·算法
【D'accumulation】15 分钟前
令牌主动失效机制范例(利用redis)注释分析
java·spring boot·redis·后端
小叶学C++21 分钟前
【C++】类与对象(下)
java·开发语言·c++
2401_8543910824 分钟前
高效开发:SpringBoot网上租赁系统实现细节
java·spring boot·后端
Cikiss33 分钟前
微服务实战——SpringCache 整合 Redis
java·redis·后端·微服务
wxin_VXbishe33 分钟前
springboot合肥师范学院实习实训管理系统-计算机毕业设计源码31290
java·spring boot·python·spring·servlet·django·php
Cikiss34 分钟前
微服务实战——平台属性
java·数据库·后端·微服务
无敌の星仔43 分钟前
一个月学会Java 第2天 认识类与对象
java·开发语言
OEC小胖胖1 小时前
Spring Boot + MyBatis 项目中常用注解详解(万字长篇解读)
java·spring boot·后端·spring·mybatis·web
2401_857617621 小时前
SpringBoot校园资料平台:开发与部署指南
java·spring boot·后端