SpringBoot 实现自定义注解 在 SpringBoot 中自定义注解是非常实用的技术常用于日志记录、权限校验、接口限流、参数校验、事务增强、统一返回处理等。核心原理注解 AOP切面实现Spring AOP 基于动态代理。下面带你从零实现一个自定义日志注解最通用、最易理解的案例。一、开发步骤1. 引入依赖需要spring-boot-starter-aop支持注解和切面!-- Spring AOP 核心依赖 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependency!-- web依赖用于测试接口 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency2. 自定义注解类创建注解LogRecord用于标记需要记录日志的方法importjava.lang.annotation.*;/** * 自定义日志注解 * Target: 注解作用目标METHOD方法 * Retention: 注解生命周期RUNTIME运行时有效 */Target(ElementType.METHOD)// 只作用在方法上Retention(RetentionPolicy.RUNTIME)// 运行时生效Documented// 生成文档publicinterfaceLogRecord{// 注解属性操作描述默认空StringoperateDesc()default;}3. 编写 AOP 切面注解核心逻辑这是真正实现注解功能的地方拦截加了LogRecord的方法importlombok.extern.slf4j.Slf4j;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.*;importorg.aspectj.lang.reflect.MethodSignature;importorg.springframework.stereotype.Component;importjava.lang.reflect.Method;Aspect// 标记为切面类Component// 交给 Spring 管理Slf4jpublicclassLogRecordAspect{/** * 切点匹配所有添加了 LogRecord 注解的方法 */Pointcut(annotation(com.example.demo.annotation.LogRecord))publicvoidlogPointCut(){}/** * 环绕通知方法执行前后都拦截 */Around(logPointCut())publicObjectaround(ProceedingJoinPointpoint)throwsThrowable{longbeginTimeSystem.currentTimeMillis();// 执行目标方法Objectresultpoint.proceed();// 执行耗时longtimeSystem.currentTimeMillis()-beginTime;// 保存日志recordLog(point,time);returnresult;}/** * 记录日志逻辑 */privatevoidrecordLog(ProceedingJoinPointjoinPoint,longtime){MethodSignaturesignature(MethodSignature)joinPoint.getSignature();Methodmethodsignature.getMethod();// 获取自定义注解LogRecordlogAnnotationmethod.getAnnotation(LogRecord.class);log.info(日志开始);// 获取注解上的描述log.info(操作描述:{},logAnnotation.operateDesc());// 获取方法名log.info(执行方法:{},signature.getDeclaringTypeName().signature.getName());// 执行耗时log.info(执行耗时:{}ms,time);log.info(日志结束);}}4. 使用自定义注解直接在Controller/Service 方法上添加LogRecordimportorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RestController;RestControllerRequestMapping(/test)publicclassTestController{/** * 使用自定义日志注解 */LogRecord(operateDesc测试自定义注解接口)GetMapping(/annotation)publicStringtestAnnotation(){return自定义注解生效啦;}}二、测试效果启动项目访问接口http://localhost:8080/test/annotation控制台输出日志开始 操作描述:测试自定义注解接口 执行方法:com.example.demo.controller.TestController.testAnnotation 执行耗时:3ms 日志结束✅自定义注解实现完成三、进阶自定义注解常用场景扩展1. 权限校验注解Target(ElementType.METHOD)Retention(RetentionPolicy.RUNTIME)publicinterfaceRequirePermission{Stringvalue();// 权限码}切面中获取当前登录用户权限 → 对比注解权限 → 无权限抛出异常。2. 接口限流注解切面中使用 Redis Lua 实现限流逻辑。3. 参数非空校验注解作用在参数/字段上切面自动校验参数是否为空。四、核心知识点总结注解三要素Target定义注解用在哪里方法/类/参数Retention定义注解生命周期必须用RUNTIMEinterface声明自定义注解AOP 五大通知Around环绕通知最常用前后都拦截Before方法执行前After方法执行后AfterReturning方法返回后AfterThrowing方法异常后核心原理Spring AOP 通过动态代理拦截带有自定义注解的方法执行增强逻辑。总结实现自定义注解 定义注解 AOP切面 使用注解必须引入spring-boot-starter-aop依赖注解本身无逻辑逻辑全在 AOP 切面中可用于日志、权限、限流、校验等场景