SpringAOP环绕通知的使用

本文详细介绍了Spring AOP中环绕通知的使用方法,包括如何通过注解和XML配置来定义环绕通知,以及如何利用ProceedingJoinPoint进行目标方法的调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇文章介绍了SpringAOP的概念以及简单使用:SpringAOP概念及其使用

在springAOP中有五种通知,环绕通知是最为强大的通知。它能够让你编写的逻辑将被通知的目标方法完全包装起来。实际上就像在一个通知方法中同时编写前置通知和后置通知。
本片文章具体讲解环绕通知的使用。

使用注解

使用环绕通知定义切面:

@Aspect
    public class AudienceAround {
        //使用@Pointcut注解声明频繁使用的切入点表达式
        @Pointcut("execution(* com.wqh.concert.Performance.perform(..))")
        public void performance(){}

        @Around("performance()")
        public void watchPerformance(ProceedingJoinPoint joinPoint){

            try {
                System.out.println("Silencing cell phones");
                System.out.println("Taking seats");
                joinPoint.proceed();
                System.out.println("Demanding a refund");

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }

        }
    }

可以看到在上面的代码中,定义通知的时候在通知方法中添加了入参:ProceedingJoinPoint。在创建环绕通知的时候,这个参数是必须写的。因为在需要在通知中使用ProceedingJoinPoint.proceed()方法调用被通知的方法。

另外,如果忘记调用proceed()方法,那么通知实际上会阻塞对被通知方法的调用。

在XML中定义

首先去掉上面类的所有注解:这里为了区别就重新创建一个类

    public class AudienceAroundXML {
        public void watchPerformance(ProceedingJoinPoint joinPoint){
            try {
                System.out.println("Silencing cell phones");
                System.out.println("Taking seats");
                joinPoint.proceed();
                System.out.println("Demanding a refund");

            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }

        }
    }

配置:

    <!--声明bean-->
    <bean name="audienceAroundXML" class="com.wqh.concert.AudienceAroundXML"/>
    <!--配置切面及通知-->
    <aop:config>
        <aop:aspect ref="audienceAroundXML">
            <aop:pointcut id="performance"
                          expression="execution(* com.wqh.concert.Performance.perform(..))"/>
            <aop:around method="watchPerformance" pointcut-ref="performance"/>
        </aop:aspect>
    </aop:config>
### Spring AOP 环绕通知 使用方法 环绕通知(`@Around`)是 Spring AOP 中功能最为强大且灵活的通知类型之一。它可以完全控制目标方法的执行过程,包括在其之前和之后的操作。以下是关于如何使用 `@Around` 通知的具体说明。 #### 定义切点 为了指定哪些方法会被拦截并应用环绕通知,可以使用 `execution()` 表达式来定义切点。例如: ```java @Pointcut("execution(* com.example.service.*.*(..))") public void serviceMethods() {} ``` 上述代码片段中的 `execution()` 表达式指定了所有位于 `com.example.service` 包下的类及其子包内的任何返回类型的任意方法都将作为切入点[^3]。 #### 编写环绕通知逻辑 通过 `@Around` 注解标记的方法即为环绕通知处理器。此方法接收一个 `ProceedingJoinPoint` 参数,该参数允许我们调用原始的目标方法,并能捕获其结果或异常处理行为。 下面是一个完整的环绕通知示例: ```java import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class LoggingAspect { @Around("serviceMethods()") public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { long start = System.currentTimeMillis(); try { System.out.println("Before method execution..."); // 调用连接点对应的实际业务方法 Object result = joinPoint.proceed(); System.out.println("After method execution successfully."); return result; } finally { long elapsedTime = System.currentTimeMillis() - start; System.out.println(joinPoint.getSignature() + " executed in " + elapsedTime + "ms"); } } } ``` 在此例子中,环绕通知不仅打印了日志消息,还计算了被增强方法的执行时间[^2]。 #### 配置启用 AspectJ 支持 要让这些配置生效,还需要确保项目已正确设置以支持 AOP 功能。通常情况下,在 Spring Boot 应用程序里只需引入依赖即可自动完成大部分基础工作;而对于传统 XML 或 Java Config 方式的应用程序,则可能需要额外声明开启全局扫描机制以便识别带有 `@Aspect` 注解的组件。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 以上展示了如何利用环绕通知解决实际开发过程中常见的需求场景——比如性能监控、事务管理或是统一的日志记录等功能实现方式[^4]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值