diff --git a/pom.xml b/pom.xml index 1126b61..b4a56d1 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,11 @@ UTF-8 + + org.springframework + spring-context + 5.3.22 + junit junit diff --git a/src/main/java/cn/zhangdeman/HashMapCache.java b/src/main/java/cn/zhangdeman/HashMapCache.java index bb0b081..7e8e342 100644 --- a/src/main/java/cn/zhangdeman/HashMapCache.java +++ b/src/main/java/cn/zhangdeman/HashMapCache.java @@ -1,17 +1,34 @@ package cn.zhangdeman; +import cn.zhangdeman.annotation.CustomExceptionScanAnnotation; +import org.springframework.context.ApplicationContext; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.util.ClassUtils; + import java.util.HashMap; import java.util.Map; // 基于hashmap实现的内存缓存 // 因为就是服务启动时初始化一次, 无需考虑多线程并发安全问题 public class HashMapCache { - // 单例: 饿汉模式 + // 单例: 懒汉模式 private static final HashMapCache hashMapCache = new HashMapCache(); private final Map cache = new HashMap<>(); //将构造器设置为private禁止通过new进行实例化 private HashMapCache() { + } + // 此处实现的是自定义注解的解析逻辑, 将错误码全部加载到内存中 + public void loadCustomException(ApplicationContext applicationContext) { + Map customExceptionAnnotationMap = applicationContext.getBeansWithAnnotation(CustomExceptionScanAnnotation.class); + for(Object bean : customExceptionAnnotationMap.values()){ + Class extensionClz = ClassUtils.getUserClass(bean); + CustomExceptionScanAnnotation customExceptionAnnotation = AnnotationUtils.findAnnotation(extensionClz, CustomExceptionScanAnnotation.class); + assert customExceptionAnnotation != null; + String[] scanPackageList = customExceptionAnnotation.exceptionPackageList(); + String[] scanExceptionClassList = customExceptionAnnotation.exceptionClassList(); + System.out.println(scanExceptionClassList, scanPackageList); + } } public static HashMapCache getHashMapCache() { return hashMapCache; diff --git a/src/main/java/cn/zhangdeman/annotation/CustomExceptionAnnotation.java b/src/main/java/cn/zhangdeman/annotation/CustomExceptionAnnotation.java index 616ea31..be716e3 100644 --- a/src/main/java/cn/zhangdeman/annotation/CustomExceptionAnnotation.java +++ b/src/main/java/cn/zhangdeman/annotation/CustomExceptionAnnotation.java @@ -2,13 +2,10 @@ package cn.zhangdeman.annotation; import java.lang.annotation.*; -// 自定义异常注解 +// 自定义异常注解: 此注解在具体异常类上上定义, 如果主入口已经定义过 `CustomExceptionScanAnnotation` 不会被重复解析 // 此注解会自动解析执行异常类, 将异常信息注册到 `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 {}; // 异常类路径, 多个类用 , 分隔 - String successCode() default "0"; // 代表成功的处理码, 默认值 0 } diff --git a/src/main/java/cn/zhangdeman/annotation/CustomExceptionCategoryAnnotation.java b/src/main/java/cn/zhangdeman/annotation/CustomExceptionCategoryAnnotation.java new file mode 100644 index 0000000..eadc9ce --- /dev/null +++ b/src/main/java/cn/zhangdeman/annotation/CustomExceptionCategoryAnnotation.java @@ -0,0 +1,11 @@ +package cn.zhangdeman.annotation; + +import java.lang.annotation.*; + +// 自定义异常注解分类 +@Retention(RetentionPolicy.RUNTIME) // 定带有注解类型的注解要保留多长时间。它需要一个RetentionPolicy参数,可以是SOURCE、CLASS或 之一RUNTIME。例如,@Retention(RetentionPolicy.RUNTIME)意味着注解应该由 JVM 保留,以便可以在运行时反射性地使用。 +@Target(ElementType.TYPE) // 注解会施加在自定义异常类之上 +@Inherited // @Inherited注解表示注解类型被自动继承。如果一个类被标注了@Inherited,那么它的子类也会继承该注解。 +public @interface CustomExceptionCategoryAnnotation { + String category() default "common"; // 异常所属分类 +} diff --git a/src/main/java/cn/zhangdeman/annotation/CustomExceptionScanAnnotation.java b/src/main/java/cn/zhangdeman/annotation/CustomExceptionScanAnnotation.java new file mode 100644 index 0000000..ef41810 --- /dev/null +++ b/src/main/java/cn/zhangdeman/annotation/CustomExceptionScanAnnotation.java @@ -0,0 +1,14 @@ +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 CustomExceptionScanAnnotation { + String[] exceptionPackageList() default {}; // 哪些包下定义了异常类, 多个包路径用 , 分隔 + String[] exceptionClassList() default {}; // 异常类路径, 多个类用 , 分隔 + String successCode() default "0"; // 代表成功的处理码, 默认值 0 +}