@Around 可以获取程序执行后的返回值吗

简介: 【8月更文挑战第13天】@Around 可以获取程序执行后的返回值吗

是的,@Around 注解可以获取程序执行后的返回值。在 @Around 注解的方法中,你可以访问到被拦截方法的返回值。以下是一个使用 Spring AOP 的例子:

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class MyAspect {
   

    @Around("execution(* com.example.service.*.*(..))")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
   
        // 在方法执行前的逻辑
        System.out.println("Before method execution");

        // 调用目标方法并获取返回值
        Object result = joinPoint.proceed();

        // 在方法执行后的逻辑
        System.out.println("After method execution");

        // 返回目标方法的返回值
        return result;
    }
}

在这个例子中,aroundAdvice 方法通过 joinPoint.proceed() 调用了目标方法,并将返回值存储在 result 变量中。然后,你可以在 aroundAdvice 方法中对返回值进行处理,或者直接将其返回给调用者。

目录
相关文章
|
存储 JSON Java
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
1144 2
|
Java Spring
Springboot starter开发之traceId请求日志链路追踪
能标识一次请求的完整流程,包括日志打印、响应标识等,以便于出现问题可以快速定位并解决问题。
2151 0
Springboot starter开发之traceId请求日志链路追踪
|
前端开发 Java 程序员
如何在swagger2中配置header请求头等参数信息?(若不会,我便手把手教你)
如何在swagger2中配置header请求头等参数信息?(若不会,我便手把手教你)
3522 1
|
SQL Java 数据库连接
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
【mybatis】第一篇,Springboot中使用插件PageHelper不生效解决方案
|
7月前
|
Java API 微服务
微服务——SpringBoot使用归纳——Spring Boot中的切面AOP处理——Spring Boot 中的 AOP 处理
本文详细讲解了Spring Boot中的AOP(面向切面编程)处理方法。首先介绍如何引入AOP依赖,通过添加`spring-boot-starter-aop`实现。接着阐述了如何定义和实现AOP切面,包括常用注解如`@Aspect`、`@Pointcut`、`@Before`、`@After`、`@AfterReturning`和`@AfterThrowing`的使用场景与示例代码。通过这些注解,可以分别在方法执行前、后、返回时或抛出异常时插入自定义逻辑,从而实现功能增强或日志记录等操作。最后总结了AOP在实际项目中的重要作用,并提供了课程源码下载链接供进一步学习。
852 0
|
搜索推荐 Java 开发者
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 问题处理
【5月更文挑战第14天】org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 问题处理
4693 1
|
XML Java 应用服务中间件
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
【Spring】运行Spring Boot项目,请求响应流程分析以及404和500报错
1156 2
sharding-jdbc 兼容 MybatisPlus的动态数据源
【8月更文挑战第2天】要使 `Sharding-JDBC` 与 `MyBatisPlus` 的动态数据源兼容,需引入相关依赖,配置数据源及分库分表策略,并在 `MyBatisPlus` 中设置参数以协同工作。可能还需自定义代码处理数据源切换。示例代码框架展示整合方式,实际应用中需按具体业务场景详细配置并处理异常情况,如数据一致性问题。
715 1
|
Java 容器
RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out问题处理
讲述RestTemplate报错I/O error on POST request for "http://crmjob.xxx.xxx.com/removeJob": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out处理方案
|
存储 安全 Java
Spring Boot中的文件下载实现
Spring Boot中的文件下载实现