From 61402bfac72b30f25816a28cd9bffdc340a83a1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Tue, 3 Jun 2025 16:08:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=B3=A8=E8=A7=A3=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91=E4=B8=8D=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 9 ++++++ pom.xml | 6 ++++ src/main/java/cn/zhangdeman/HashMapCache.java | 31 +++++++++++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..a738bab --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# 使用方式 + +在主入口函数增加如下注解: + +```bash +@Import(value = { cn.zhangdeman.HashMapCache.class}) // 让SpringBoot能够扫描到相关组件 +``` + +会自动扫描自定义的异常注解, 并进行数据初始化 diff --git a/pom.xml b/pom.xml index 05f9c40..2500ad2 100644 --- a/pom.xml +++ b/pom.xml @@ -24,6 +24,12 @@ lombok 1.18.30 + + jakarta.servlet + jakarta.servlet-api + 6.1.0 + provided + junit junit diff --git a/src/main/java/cn/zhangdeman/HashMapCache.java b/src/main/java/cn/zhangdeman/HashMapCache.java index ce6ca93..a7ba6b9 100644 --- a/src/main/java/cn/zhangdeman/HashMapCache.java +++ b/src/main/java/cn/zhangdeman/HashMapCache.java @@ -1,6 +1,9 @@ package cn.zhangdeman; import cn.zhangdeman.annotation.CustomExceptionScanAnnotation; +import jakarta.servlet.ServletContextEvent; +import jakarta.servlet.ServletContextListener; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.ClassUtils; @@ -12,16 +15,18 @@ import java.util.*; // 基于hashmap实现的内存缓存 // 因为就是服务启动时初始化一次, 无需考虑多线程并发安全问题 -public class HashMapCache { +public class HashMapCache implements ServletContextListener { // 单例: 饿汉模式 private static final HashMapCache hashMapCache = new HashMapCache(); private final Map cache = new HashMap<>(); + @Autowired + private ApplicationContext applicationContext; //将构造器设置为private禁止通过new进行实例化 private HashMapCache() { } // 此处实现的是自定义注解的解析逻辑, 将错误码全部加载到内存中 - public void loadCustomException(ApplicationContext applicationContext) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException { + public void loadCustomException() throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException { Map customExceptionScanAnnotationMap = applicationContext.getBeansWithAnnotation(CustomExceptionScanAnnotation.class); for(Object bean : customExceptionScanAnnotationMap.values()){ Class extensionClz = ClassUtils.getUserClass(bean); @@ -117,4 +122,26 @@ public class HashMapCache { } return classList; } + + @Override + public void contextInitialized(ServletContextEvent sce) { + System.out.println("包内自动触发初始化" + new Date()); + try { + this.loadCustomException(); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } catch (InvocationTargetException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (NoSuchMethodException e) { + throw new RuntimeException(e); + } catch (InstantiationException e) { + throw new RuntimeException(e); + } + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + } }