From fe169aa12f4235cf99d0d2a1f094256e9523e7d5 Mon Sep 17 00:00:00 2001 From: cuijiawang Date: Thu, 27 Mar 2025 14:05:35 +0800 Subject: [PATCH] =?UTF-8?q?feat(stu.base.annotation):=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=AF=B7=E6=B1=82=E6=96=B9=E6=B3=95=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E7=9A=84=E5=B7=A5=E5=85=B7=E7=B1=BB-=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=20AnnotationHelper=20=E7=B1=BB=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E4=BB=8E=20HttpServletRequest=20=E4=B8=AD=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=A4=84=E7=90=86=E5=BD=93=E5=89=8D=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95=E4=B8=8A=E7=9A=84=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=20-=20=E8=AF=A5=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E4=BD=BF=E7=94=A8=20Spring=20=E7=9A=84=20AnnotationUt?= =?UTF-8?q?ils=20=E5=92=8C=20HandlerMethod=20=E6=9D=A5=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=E7=9A=84=E6=9F=A5=E6=89=BE=20-=20=E5=8F=AF?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E5=9C=A8=E6=8B=A6=E6=88=AA=E5=99=A8=E6=88=96?= =?UTF-8?q?=E5=85=B6=E4=BB=96=E5=9C=B0=E6=96=B9=E5=BF=AB=E9=80=9F=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=96=B9=E6=B3=95=E7=BA=A7=E5=88=AB=E7=9A=84=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../stu/base/annotation/AnnotationHelper.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/stu/base/annotation/AnnotationHelper.java diff --git a/src/main/java/stu/base/annotation/AnnotationHelper.java b/src/main/java/stu/base/annotation/AnnotationHelper.java new file mode 100644 index 0000000..18d19f2 --- /dev/null +++ b/src/main/java/stu/base/annotation/AnnotationHelper.java @@ -0,0 +1,32 @@ +package stu.base.annotation; + +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.HandlerMapping; + +import javax.servlet.http.HttpServletRequest; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; + +public class AnnotationHelper { + + /** + * 从 HttpServletRequest 中获取处理当前请求的方法上的注解 + */ + public static T getMethodAnnotation( + HttpServletRequest request, + Class annotationClass + ) { + // 从请求属性中获取 HandlerMethod + HandlerMethod handlerMethod = (HandlerMethod) request.getAttribute( + HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE + ); + + if (handlerMethod != null) { + Method method = handlerMethod.getMethod(); + // 获取方法上的注解(包括继承的注解) + return AnnotationUtils.findAnnotation(method, annotationClass); + } + return null; + } +}