AOP之环绕通知@Around

AOP之环绕通知@Around

@Around是Spring AOP中环绕通知的注解,作用是定义一个切面,用于在目标方法之前和之后执行一些额外的逻辑。

另外,@Around也是自定义注解时需要使用的注解。

@Around 方法必须接收一个 ProceedingJoinPoint 参数,以便通过 proceed() 调用目标方法。

环绕通知定义

复制代码
@Aspect
@Component
public class LogExectionTimeAspect {

    /**
     * 环绕通知的具体业务逻辑
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    // 环绕通知注解 @Around
    @Around("execution(* com.example.service.*.*(..))")
    public Object logExectionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("触发前: "+ System.currentTimeMillis());
        /**
         * proceed 函数用于调用执行逻辑
         * 是环绕通知(@Around)的关键方法,
         * 更多的作用是在环绕通知中根据某些逻辑阻止方法执行或者修改方法的参数和返回值
         * 返回值便是原函数返回值
         */
        Object proceed = joinPoint.proceed(); // 执行目标方法
        // 获取目标参数示例
        // Object[] args = joinPoint.getArgs();  // 获取目标方法的参数
        // do somthing
        // 使用修改后的参数
        // Object result = joinPoint.proceed(args);
        joinPoint.getSignature();  // 该函数可以获取方法相关信息
        System.out.println("触发后: "+ System.currentTimeMillis());
        return proceed;
    }
}

自定义注解

复制代码
/**
 * 自定义注解起始
 * 1. 创建自定义注解,即该接口
 * 2. 实现注解功能
 */
@Target(ElementType.METHOD) // 可以用于方法上
@Retention(RetentionPolicy.RUNTIME) // 注解在运行时可用
public @interface LogExectionTime {
}

自定义注解实现:

复制代码
/**
 * 自定义注解实现类
 */
@Aspect
@Component
public class LogExectionTimeAspect {

    /**
     * 环绕通知的具体业务逻辑
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    // 环绕通知注解 @Around
    @Around("@annotation(com.example.demo.aop.LogExectionTime)") // 此处路径就是创建的@interface的路径
    public Object logExectionTime(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("触发前: "+ System.currentTimeMillis());
       // do something
       // ...
        Object proceed = joinPoint.proceed(); // 执行目标方法
        joinPoint.getSignature();
        System.out.println("触发后: "+ System.currentTimeMillis());
        return proceed;
    }
}

说明

execution用于指定监听范围。execution可以有很多写法,下面会介绍几种。

"execution(public * com.example.demo.service.impl.AccountServiceImpl.aopBeforeMsg(...))"

"execution(* com.example.demo.service.impl.AccountServiceImpl.aopBeforeMsg(...))"

以上两种写法都表明监听com.example.demo.service.impl.AccountServiceImpl类下的aopBeforeMsg函数。

"execution(public * com.example.demo.service.impl.AccountServiceImpl.*(...))"

以上表明监听AccountServiceImpl类下的所有函数。

"execution(public * com.example.demo.service.impl.AccountServiceImpl.get*(...))"

以上写法表明监听AccountServiceImpl类下所有get开头函数。

"execution(public * com.example.demo.service.impl.AccountServiceImpl.get*(...)) || || execution(* com.example.service.OrderService.get*(...)) "

以上写法表示监听AccountServiceImpl类与OrderService类中get开头的函数。

"execution(* com.example.service...*(...))"

以上写法表示监听service包下的所有函数。

注意点

@Around也只能监听public和protected修饰的函数,并且实例必须是依赖Spring管理。

相关推荐
shushangyun_8 分钟前
批发商城系统源码多少钱?2026最新报价一览
java·开发语言·人工智能·spring·spring cloud
cfm_291410 分钟前
JVM深度详解:Class常量池、运行时常量池、字符串常量池、包装类对象池
java·jvm
JAVA面经实录91710 分钟前
高频算法面试题
java·计算机网络·算法·面试
影视飓风TIM11 分钟前
从C++引用到类封装:底层视角拆解核心语法与面试考点
java·开发语言
小易撩挨踢22 分钟前
[特殊字符] Spring AI 2.0.0 正式发布:大版本升级,MCP 原生集成 + Anthropic SDK 全线重构
人工智能·spring·重构
llz_11230 分钟前
web-第六次课后作业
前端·spring boot·后端
天天爱吃肉821841 分钟前
豆包 vs DeepSeek API 对比分析报告
android·java·大数据·开发语言·功能测试·嵌入式硬件·汽车
柏舟飞流43 分钟前
Spring Boot + Spring Security + RBAC:从登录鉴权到权限模型设计
java·spring boot·spring
AC赳赳老秦1 小时前
OpenClaw + 飞书多维表格:自动同步数据、生成统计图表、触发自动化任务
java·大数据·python·缓存·自动化·deepseek·openclaw
CS_SKILL1 小时前
吉比特 C++ 实习一面面经:一轮把 C++、容器、并发、排序和网络全扫了一遍
java·开发语言·校招面经·实习面经·技术面经·吉比特校招