feat(plugin): fix multiple implementation class scans by adding only one

This commit is contained in:
qianmoQ 2024-11-22 11:45:05 +08:00
parent e4a3f19a04
commit bbb8b65a16
4 changed files with 35 additions and 9 deletions

View File

@ -427,6 +427,7 @@ public class PluginManager
Path pluginsPath = config.getPluginsDir();
if (Files.isRegularFile(pluginsPath)) {
log.debug("Loading plugin from file: {}", pluginsPath);
// 如果是文件直接加载
// Load plugin from file
loadPluginFromDirectory(pluginsPath);
@ -455,10 +456,13 @@ public class PluginManager
// 从目录名获取插件基本信息
// Get plugin basic information from directory name
String pluginBaseName = pluginDir.getFileName().toString();
log.debug("Found plugin directory: {}", pluginDir);
log.debug("Found plugin: {}", pluginBaseName);
// 获取插件版本(可以从配置文件或清单文件中读取)
// Get plugin version (can be read from config or manifest file)
String pluginVersion = getPluginVersion(pluginDir);
log.debug("Found plugin version: {}", pluginVersion);
// 创建插件专用类加载器
// Create plugin-specific class loader

View File

@ -56,6 +56,10 @@ public class ServiceSpiLoader
String servicePath = "META-INF/services/" + serviceType.getName();
Enumeration<URL> resources = classLoader.getResources(servicePath);
// 添加一个 Set 来跟踪已处理的实现类
// Add a Set to track processed implementation classes
Set<String> processedImplementations = Sets.newHashSet();
boolean found = false;
while (resources.hasMoreElements()) {
found = true;
@ -67,6 +71,15 @@ public class ServiceSpiLoader
while ((line = reader.readLine()) != null) {
if (!line.trim().isEmpty() && !line.startsWith("#")) {
line = line.trim();
// 如果这个实现类已经处理过跳过
// Skip if this implementation class has already been processed
if (processedImplementations.contains(line)) {
log.debug("Skipping already processed implementation class: {}", line);
continue;
}
processedImplementations.add(line);
log.debug("Service implementation defined in file: {}", line);
try {
// 使用指定的类加载器加载实现类
@ -81,10 +94,14 @@ public class ServiceSpiLoader
// 添加直接绑定
// Add direct binding
if (serviceType.isAssignableFrom(serviceImpl)) {
if (!bindings.getBindings().containsKey(serviceType)) {
bindings.addBinding(serviceType, serviceImpl);
log.debug("Added direct binding: {} -> {}", serviceType.getName(), serviceImpl.getName());
}
// 导致无法进行多个实现类扫描
// Not causing multiple implementation scanning
// if (!bindings.getBindings().containsKey(serviceType)) {
// bindings.addBinding(serviceType, serviceImpl);
// log.debug("Added direct binding: {} -> {}", serviceType.getName(), serviceImpl.getName());
// }
bindings.addBinding(serviceType, serviceImpl);
log.debug("Added direct binding: {} -> {}", serviceType.getName(), serviceImpl.getName());
}
// 检查并添加接口绑定
@ -93,10 +110,14 @@ public class ServiceSpiLoader
if (serviceType.isAssignableFrom(iface)) {
@SuppressWarnings("unchecked")
Class<? extends Service> serviceInterface = (Class<? extends Service>) iface;
if (!bindings.getBindings().containsKey(serviceInterface)) {
bindings.addBinding(serviceInterface, serviceImpl);
log.debug("Added interface binding: {} -> {}", serviceInterface.getName(), serviceImpl.getName());
}
// 导致无法进行多个实现类扫描
// Not causing multiple implementation scanning
// if (!bindings.getBindings().containsKey(serviceInterface)) {
// bindings.addBinding(serviceInterface, serviceImpl);
// log.debug("Added interface binding: {} -> {}", serviceInterface.getName(), serviceImpl.getName());
// }
bindings.addBinding(serviceInterface, serviceImpl);
log.debug("Added interface binding: {} -> {}", serviceInterface.getName(), serviceImpl.getName());
}
}
}

View File

@ -1,2 +1,2 @@
io.edurt.datacap.test.ConsoleService
io.edurt.datacap.test.LogService
io.edurt.datacap.test.LogService

View File

@ -22,6 +22,7 @@ public class DataServiceTest
Path projectRoot = PluginPathUtils.findProjectRoot();
PluginConfigure config = PluginConfigure.builder()
.pluginsDir(projectRoot.resolve("test/datacap-test-plugin"))
.scanDepth(3)
.build();
pluginManager = new PluginManager(config);