Ver Fonte

data-upgrade 数据升级服务接口至少存在一个匹配目标版本号的实现

deason há 3 meses atrás
pai
commit
dee5b3f7c2

+ 21 - 16
data-upgrade/src/main/java/com/qmth/boot/data/upgrade/config/DataUpgradeListener.java

@@ -126,10 +126,6 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
             }
 
             Map<String, DataUpgradeService> serviceMaps = this.matchServices(appInfo.getAppVersion(), properties.getAppVersion());
-            if (serviceMaps.isEmpty()) {
-                log.warn("未找到数据升级的具体实现!{}:{}", BASE_PACKAGE, properties.getBasePackage());
-                throw new RuntimeException("未找到数据升级的具体实现!");
-            }
 
             // 按版本号由小到大排序
             List<String> versionKeys = new ArrayList<>(serviceMaps.keySet());
@@ -163,16 +159,14 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
     /**
      * 获取数据升级的具体实现集合
      */
-    private Map<String, DataUpgradeService> matchServices(String currentVersion, String appVersion) {
-        List<Class<?>> implClasses = ClassHelper.findInterfaceImpls(DataUpgradeService.class, properties.getBasePackage());
-        if (implClasses.isEmpty()) {
-            return new HashMap<>();
-        }
-
+    private Map<String, DataUpgradeService> matchServices(String currentVersion, String targetVersion) {
+        boolean existTargetVersionImpl = false;
         VersionComparator vc = new VersionComparator();
         Map<String, DataUpgradeService> serviceMaps = new HashMap<>();
-        for (Class<?> clazz : implClasses) {
-            DataUpgradeVersion annotation = clazz.getAnnotation(DataUpgradeVersion.class);
+
+        List<Class<?>> implClasses = ClassHelper.findInterfaceImpls(DataUpgradeService.class, properties.getBasePackage());
+        for (Class<?> implClass : implClasses) {
+            DataUpgradeVersion annotation = implClass.getAnnotation(DataUpgradeVersion.class);
             if (annotation == null) {
                 // 跳过未声明“数据升级版本”注解的实现
                 continue;
@@ -180,17 +174,22 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
 
             String implVersion = annotation.value();
             if (!VersionComparator.isValidVersion(implVersion)) {
-                throw new RuntimeException("实现类声明的版本号格式不正确!" + clazz.getSimpleName() + " @DataUpgradeVersion:" + implVersion);
+                throw new RuntimeException("实现类声明的版本号格式不正确!" + implClass.getSimpleName() + " @DataUpgradeVersion:" + implVersion);
             }
 
-            if (vc.compare(implVersion, currentVersion) > 0 && vc.compare(implVersion, appVersion) <= 0) {
-                // log.info("{} {}", clazz.getSimpleName(), implVersion);
+            int targetVersionCompare = vc.compare(implVersion, targetVersion);
+            if (targetVersionCompare == 0) {
+                existTargetVersionImpl = true;
+            }
+
+            if (vc.compare(implVersion, currentVersion) > 0 && targetVersionCompare <= 0) {
+                log.debug("implClass:{} implVersion:{}", implClass.getSimpleName(), implVersion);
                 if (serviceMaps.containsKey(implVersion)) {
                     throw new RuntimeException("同个版本数据升级的实现出现重复!@DataUpgradeVersion:" + implVersion);
                 }
 
                 try {
-                    Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(clazz);
+                    Constructor<?> constructor = ClassUtils.getConstructorIfAvailable(implClass);
                     if (constructor != null) {
                         serviceMaps.put(implVersion, (DataUpgradeService) constructor.newInstance());
                     }
@@ -199,6 +198,12 @@ public class DataUpgradeListener implements ApplicationListener<ApplicationPrepa
                 }
             }
         }
+
+        if (!existTargetVersionImpl) {
+            log.warn("未找到版本“{}”数据升级的具体实现!{}:{}", targetVersion, BASE_PACKAGE, properties.getBasePackage());
+            throw new RuntimeException("未找到数据升级的具体实现!");
+        }
+
         return serviceMaps;
     }