更换扫描注解的方式

This commit is contained in:
Yutousama 2022-07-16 15:59:20 +08:00
parent e085690446
commit 67587d208b
3 changed files with 12 additions and 12 deletions

View File

@ -50,9 +50,11 @@ public abstract class Model implements ModelInterface {
}
public static List<Class<?>> classList = new ArrayList<>();
public static List<Class<?>> classList;
long group;
static {
classList=new ArrayList<>();
}
public Model() {
if (!classList.contains(getClass())) {
classList.add(getClass());

View File

@ -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<Class> scanClass(String classPath, Class<? extends Annotation> annotation) {
List<Class> classList = new ArrayList<>();
public static List<Class<?>> scanClass(String classPath, Class<? extends Annotation> annotation) {
List<Class<?>> classList = new ArrayList<>();
if (ObjectUtils.isEmpty(classPath)) {
return classList;
}
ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false);
TypeFilter includeFilter = (metadataReader, metadataReaderFactory) -> true;
provider.addIncludeFilter(includeFilter);
Set<BeanDefinition> beanDefinitionSet = new HashSet<>();
// 指定扫描的包名
Set<BeanDefinition> candidateComponents = provider.findCandidateComponents(classPath);
beanDefinitionSet.addAll(candidateComponents);
Set<BeanDefinition> 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))) {

View File

@ -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<Class<?>> 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 = "";