Java-39 深入浅出 Spring - AOP切面增强 核心概念 通知类型 XML+注解方式 附代码

点一下关注吧!!!非常感谢!!持续更新!!!

🚀 AI篇持续更新中!(长期更新)

目前2025年06月05日更新到:
AI炼丹日志-28 - Audiblez 将你的电子书epub转换为音频mp3 做有声书,持续打造实用AI工具指南!📐🤖

💻 Java篇正式开启!(300篇)

目前2025年06月05日更新到:
Java-38 深入浅出 Spring - AOP切面增强 核心概念 相关术语 Proxy配置

MyBatis 已完结,Spring 正在火热更新中,深入浅出助你打牢基础!

📊 大数据板块已完成多项干货更新(300篇):

包括 Hadoop、Hive、Kafka、Flink、ClickHouse、Elasticsearch 等二十余项核心组件,覆盖离线+实时数仓全栈!

目前2025年06月05日更新到:
大数据-278 Spark MLib - 基础介绍 机器学习算法 梯度提升树 GBDT案例 详解

👉 点个关注,不迷路!后续还将持续更新更多大模型+数据智能+工程实战内容,敬请期待!

上节进度

上节我们更新到了 Spring AOP 的部分,到了"改变代理的配置"的内容。

下面我们继续。

AOP实现

略过上节内容

通知类型

前置通知

aop:before

shell 复制代码
<aop:before method="printLog" pointcut-ref="pointcut">
  
</aop:before>

● method 用于指定前置通知的方法名称

● pointcut 用于指定切入点表达式

● pointcut-ref 用于指定切入点表达式的作用

作用:用于配置前置通知

出现位置:aop:aspect 的内部

执行时机:前置通知永远都会在切入点方法(业务核心方法)执行之前执行

细节:前置通知可以获取切入点方法的参数,并对其进行增强

异常通知

aop:after-throwing

shell 复制代码
<aop:after-throwing method="afterThrowingPrintLog" pointcut-ref="pointcut'>
  
</aop:after-throwing>

用于配置异常通知

出现的位置:它只能出现在 aop:aspect 标签内部

执行时机:异常通知的执行时机是在切入点方法(业务核心方法)执行产生异常之后,异常通知执行,如果切入点方法执行没有产生异常,则异常通知不会执行

细节:异常通知不仅可以获取切入点方法执行的参数,也可以获取切入点方法执行产生的异常信息

最终通知

用于指定最终通知。

出现的位置:它只能在 aop:aspect 标签内部

shell 复制代码
<aop:after method="afterPrintLog" pointcut-ref="pointcut">
</aop:after>

执行时机:最终通知的执行时机切入点方法(业务核心方法)执行完成之后,切入点方法返回之前执行,换句话说,无论切入点方法执行是否产生异常,它都会在返回之前执行。

细节:最终通知环节,可以获取到通知方法的参数,通知它可以做一些清理操作。

环绕通知

配置方式:

shell 复制代码
<aop:around method="aroundPrintLog" pointcut-ref="pt1"></aop:around>

作用:用于配置环绕通知

出现位置:只能出现在 aop:aspect标签的内部

属性:

● method:用于指定环绕通知的方法名称

● pointcut:用于指定切入点表达式

● pointcut-ref:用于指定切入点表达式的引用

环绕通知,它是有别于面前四种通知类型外的特殊通知,前面四种通知(前置、后置、异常和最终)它们都是指定何时增强的通知类型。而环绕通知,它是Spring框架为我们提供的一种可以通过编码的方式,控制增强代码何时执行的通知类型。它里面借助ProceedingJoinPoint接口以及实现类,实现手动触发切入点方法的调用。

XML+注解方式

开启注解

shell 复制代码
<!-- 开启spring对注解aop的⽀持 -->
<aop:aspectj-autoproxy/>

我们需要修改 applicationContext.xml

对应的截图如下所示:

示例代码

WzkLogUtils

java 复制代码
/**
 * WzkLogUtils
 * @author wzk
 * @date 11:26 2025/1/6
**/
@Component
@Aspect
public class WzkLogUtils {

    /**
     * 在引用切入点表达式的时候,必须是方法名+(),例如 pointcut()
     * 在当前切面中使用,可以直接写方法名,在其他切面中使用的必须是全限定方法名
     * @author wzk
     * @date 11:28 2025/1/6
    **/
    @Pointcut("execution(* wzk.service.impl.*.*(..))")
    public void pointcut() {

    }

    /**
     * 前置通知
     * @author wzk
     * @date 13:50 2025/1/6
    **/
    @Before("pointcut()")
    public void beforePrintLog(JoinPoint jp) {
        Object[] args = jp.getArgs();
        System.out.println("前置通知: beforePrintLog: " + Arrays.toString(args));
    }

    /**
     * 异常通知
     * @author wzk
     * @date 13:50 2025/1/6
     **/
    @AfterThrowing(value = "pointcut()", throwing = "e")
    public void afterThrowingPrintLog(JoinPoint joinPoint, Throwable e) {
        System.out.println("后置通知: afterThrowingPrintLog: " + joinPoint.getSignature().getName() + " 异常: " + e.getMessage());
    }

    /**
     * 后置通知
     * @author wzk
     * @date 13:50 2025/1/6
     **/
    @After("pointcut()")
    public void afterPrintLog(JoinPoint joinPoint) {
        System.out.println("后置通知: afterPrintLog: " + joinPoint.getSignature().getName());
    }

    /**
     * 环绕通知
     * @author wzk
     * @date 13:50 2025/1/6
     **/
    @Around("pointcut()")
    public Object aroundPrintLog(ProceedingJoinPoint proceedingJoinPoint) {
        Object result = null;
        try {
            System.out.println("环绕通知: beforePrintLog: " + proceedingJoinPoint.getSignature().getName());
            result = proceedingJoinPoint.proceed();
            System.out.println("环绕通知: afterPrintLog: " + proceedingJoinPoint.getSignature().getName());
        } catch (Throwable e) {
            System.out.println("异常通知");
            e.printStackTrace();
        } finally {
            System.out.println("最终通知");
        }
        return result;
    }

    public void printLog() {
        System.out.println("WzkLogUtils: printLog");
    }

}

对应的文件路径如下所示:

对应的代码的内容如下所示:

注解模式

我们在使用注解驱动开发AOP时,我们要明确的是,是注解替换配置文件中的下面的这个配置:

shell 复制代码
<!--开启spring对注解aop的⽀持-->
<aop:aspectj-autoproxy/>

这个配置后续我们就不需要了, 所以需要在啊 applicationContext.xml 中注释掉。

结果如下所示:

在配置类中使用如下注解进行替换上述配置:

java 复制代码
 /**
 * WzkAopConfig
 * @author wzk
 * @date 13:59 2025/1/6
**/
@Configuration
@Component("wzk")
@EnableAspectJAutoProxy
public class WzkAopConfig {
    
}

对应的结果如下所示:

相关推荐
_r0bin_27 分钟前
前端面试准备-7
开发语言·前端·javascript·fetch·跨域·class
zhang988000028 分钟前
JavaScript 核心原理深度解析-不停留于表面的VUE等的使用!
开发语言·javascript·vue.js
恸流失29 分钟前
DJango项目
后端·python·django
硅的褶皱2 小时前
对比分析LinkedBlockingQueue和SynchronousQueue
java·并发编程
MoFe12 小时前
【.net core】天地图坐标转换为高德地图坐标(WGS84 坐标转 GCJ02 坐标)
java·前端·.netcore
zdkdchao2 小时前
hbase资源和数据权限控制
大数据·数据库·hbase
季鸢2 小时前
Java设计模式之观察者模式详解
java·观察者模式·设计模式
归去_来兮2 小时前
知识图谱技术概述
大数据·人工智能·知识图谱
Fanxt_Ja2 小时前
【JVM】三色标记法原理
java·开发语言·jvm·算法
萌新小码农‍2 小时前
Spring框架学习day7--SpringWeb学习(概念与搭建配置)
学习·spring·状态模式