AOP入门程序

AOP是一种编程思想,而spring框架对这种思想进行实现,那我们学习的就是SpringAOP.

AOP是面向切面编程

AOP快速入门:

1.引入AOP依赖

入门程序:

java 复制代码
package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
@Aspect //表示当前为AOP类
@Component
@Slf4j
public class RecordTimeAspect {

    @Around("execution(* com.itheima.service.impl.*.*(..))")
    public Object recordTime(ProceedingJoinPoint pjp) throws Throwable {
        //1.记录方法运行时间
        long begin = System.currentTimeMillis();

        //2.执行原始的方法
        Object result = pjp.proceed();


        //3.记录方法的结束时间,记录耗时
        long end = System.currentTimeMillis();
        log.info("方法{}执行耗时:{}ms",pjp.getSignature(),end-begin);

        return result;
    }
}
相关推荐
014-code18 小时前
订单超时取消与库存回滚的完整实现(延迟任务 + 状态机)
java·开发语言
lly20240618 小时前
组合模式(Composite Pattern)
开发语言
游乐码18 小时前
c#泛型约束
开发语言·c#
Dontla19 小时前
go语言Windows安装教程(安装go安装Golang安装)(GOPATH、Go Modules)
开发语言·windows·golang
chushiyunen19 小时前
python rest请求、requests
开发语言·python
铁东博客19 小时前
Go实现周易大衍筮法三变取爻
开发语言·后端·golang
baidu_huihui19 小时前
在 CentOS 9 上安装 pip(Python 的包管理工具)
开发语言·python·pip
南 阳19 小时前
Python从入门到精通day63
开发语言·python
lbb 小魔仙19 小时前
Python_RAG知识库问答系统实战指南
开发语言·python
java1234_小锋19 小时前
Java高频面试题:Springboot的自动配置原理?
java·spring boot·面试