From abd808b9f131b8e3a8b777e259ae44fbf48c146d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 2 Jun 2025 07:13:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E5=BC=82=E5=B8=B8=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E4=BB=A5=E5=8F=8A=E6=8E=A5=E5=8F=A3=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/zhangdeman/ICustomExceptionAnnotation.java | 9 +++++++++ .../annotation/CustomExceptionAnnotation.java | 13 +++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/main/java/cn/zhangdeman/ICustomExceptionAnnotation.java create mode 100644 src/main/java/cn/zhangdeman/annotation/CustomExceptionAnnotation.java diff --git a/src/main/java/cn/zhangdeman/ICustomExceptionAnnotation.java b/src/main/java/cn/zhangdeman/ICustomExceptionAnnotation.java new file mode 100644 index 0000000..d8b1f95 --- /dev/null +++ b/src/main/java/cn/zhangdeman/ICustomExceptionAnnotation.java @@ -0,0 +1,9 @@ +package cn.zhangdeman; + +import java.util.Map; + +// 为了支持注解扫描, 自定义exception必须实现此注解 +public interface ICustomExceptionAnnotation { + // 获取错误码列表: code => message + public Map getCodeTable(); +} diff --git a/src/main/java/cn/zhangdeman/annotation/CustomExceptionAnnotation.java b/src/main/java/cn/zhangdeman/annotation/CustomExceptionAnnotation.java new file mode 100644 index 0000000..a31ebf0 --- /dev/null +++ b/src/main/java/cn/zhangdeman/annotation/CustomExceptionAnnotation.java @@ -0,0 +1,13 @@ +package cn.zhangdeman.annotation; + +import java.lang.annotation.*; + +// 自定义异常注解 +// 此注解会自动解析执行异常类, 将异常信息注册到 `HashMapCache` 中 +@Retention(RetentionPolicy.RUNTIME) // 定带有注解类型的注解要保留多长时间。它需要一个RetentionPolicy参数,可以是SOURCE、CLASS或 之一RUNTIME。例如,@Retention(RetentionPolicy.RUNTIME)意味着注解应该由 JVM 保留,以便可以在运行时反射性地使用。 +@Target(ElementType.TYPE) // 注解会施加在自定义异常类之上 +@Inherited // @Inherited注解表示注解类型被自动继承。如果一个类被标注了@Inherited,那么它的子类也会继承该注解。 +public @interface CustomExceptionAnnotation { + String exceptionPackageList() default ""; // 哪些包下定义了异常类, 多个包路径用 , 分隔 + String exceptionClassList() default ""; // 异常类路径, 多个类用 , 分隔 +}