定义异常注解以及接口约束

This commit is contained in:
白茶清欢 2025-06-02 07:13:23 +08:00
parent a01dfaf671
commit abd808b9f1
2 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package cn.zhangdeman;
import java.util.Map;
// 为了支持注解扫描, 自定义exception必须实现此注解
public interface ICustomExceptionAnnotation {
// 获取错误码列表: code => message
public Map<Object, String> getCodeTable();
}

View File

@ -0,0 +1,13 @@
package cn.zhangdeman.annotation;
import java.lang.annotation.*;
// 自定义异常注解
// 此注解会自动解析执行异常类, 将异常信息注册到 `HashMapCache`
@Retention(RetentionPolicy.RUNTIME) // 定带有注解类型的注解要保留多长时间它需要一个RetentionPolicy参数可以是SOURCECLASS或 之一RUNTIME例如@Retention(RetentionPolicy.RUNTIME)意味着注解应该由 JVM 保留以便可以在运行时反射性地使用
@Target(ElementType.TYPE) // 注解会施加在自定义异常类之上
@Inherited // @Inherited注解表示注解类型被自动继承如果一个类被标注了@Inherited那么它的子类也会继承该注解
public @interface CustomExceptionAnnotation {
String exceptionPackageList() default ""; // 哪些包下定义了异常类, 多个包路径用 , 分隔
String exceptionClassList() default ""; // 异常类路径, 多个类用 , 分隔
}