diff --git a/src/main/java/com/yutou/qqbot/models/Model.java b/src/main/java/com/yutou/qqbot/models/Model.java index 1d2f95c..f871a88 100644 --- a/src/main/java/com/yutou/qqbot/models/Model.java +++ b/src/main/java/com/yutou/qqbot/models/Model.java @@ -50,9 +50,11 @@ public abstract class Model implements ModelInterface { } - public static List> classList = new ArrayList<>(); + public static List> classList; long group; - + static { + classList=new ArrayList<>(); + } public Model() { if (!classList.contains(getClass())) { classList.add(getClass()); diff --git a/src/main/java/com/yutou/qqbot/utlis/AppTools.java b/src/main/java/com/yutou/qqbot/utlis/AppTools.java index d578619..9bb9379 100644 --- a/src/main/java/com/yutou/qqbot/utlis/AppTools.java +++ b/src/main/java/com/yutou/qqbot/utlis/AppTools.java @@ -1,8 +1,10 @@ package com.yutou.qqbot.utlis; +import com.alibaba.fastjson2.JSON; import com.yutou.qqbot.QQBotManager; import com.yutou.qqbot.interfaces.DownloadInterface; import com.yutou.qqbot.interfaces.ObjectInterface; +import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.core.annotation.AnnotationUtils; @@ -23,6 +25,7 @@ import java.text.SimpleDateFormat; import java.util.*; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import java.util.logging.Level; public class AppTools { @@ -83,22 +86,21 @@ public class AppTools { return System.getProperty("os.name").contains("Windows"); } - public static List scanClass(String classPath, Class annotation) { - List classList = new ArrayList<>(); + public static List> scanClass(String classPath, Class annotation) { + List> classList = new ArrayList<>(); if (ObjectUtils.isEmpty(classPath)) { return classList; } ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); TypeFilter includeFilter = (metadataReader, metadataReaderFactory) -> true; provider.addIncludeFilter(includeFilter); - Set beanDefinitionSet = new HashSet<>(); // 指定扫描的包名 Set candidateComponents = provider.findCandidateComponents(classPath); - beanDefinitionSet.addAll(candidateComponents); + Set beanDefinitionSet = new HashSet<>(candidateComponents); beanDefinitionSet.forEach(beanDefinition -> { try { - Class clazz = Class.forName(beanDefinition.getBeanClassName()); + Class clazz = Class.forName(beanDefinition.getBeanClassName()); if (!ObjectUtils.isEmpty(annotation)) { if (!ObjectUtils.isEmpty(AnnotationUtils.getAnnotation(clazz, annotation))) { diff --git a/src/main/java/com/yutou/qqbot/utlis/ApplicationInit.java b/src/main/java/com/yutou/qqbot/utlis/ApplicationInit.java index 0461f92..da872df 100644 --- a/src/main/java/com/yutou/qqbot/utlis/ApplicationInit.java +++ b/src/main/java/com/yutou/qqbot/utlis/ApplicationInit.java @@ -6,14 +6,12 @@ import com.yutou.qqbot.QQNumberManager; import com.yutou.qqbot.models.Model; import net.mamoe.mirai.Bot; import net.mamoe.mirai.contact.Group; -import org.reflections.Reflections; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.Date; -import java.util.Set; import java.util.Timer; import java.util.TimerTask; @@ -21,9 +19,7 @@ import java.util.TimerTask; public class ApplicationInit implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { - Reflections ref = new Reflections("com.yutou.qqbot.models"); - Set> annotatedWith = ref.getTypesAnnotatedWith(UseModel.class); - Model.classList.addAll(annotatedWith); + Model.classList.addAll(AppTools.scanClass("com.yutou.qqbot.models", UseModel.class)); new Timer().schedule(new TimerTask() { private String oldTime = "";