增加异常所属分类注解 + 规划注解解析的逻辑
This commit is contained in:
parent
fc48259497
commit
e017917f16
5
pom.xml
5
pom.xml
@ -14,6 +14,11 @@
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.3.22</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
|
@ -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<Object, String> cache = new HashMap<>();
|
||||
//将构造器设置为private禁止通过new进行实例化
|
||||
private HashMapCache() {
|
||||
|
||||
}
|
||||
// 此处实现的是自定义注解的解析逻辑, 将错误码全部加载到内存中
|
||||
public void loadCustomException(ApplicationContext applicationContext) {
|
||||
Map<String, Object> 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;
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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"; // 异常所属分类
|
||||
}
|
@ -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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user