组件类获取优化

V0.5.x
xiwa 2022-11-23 09:11:55 +08:00
parent 732fe5b6ef
commit 629f4f551d
1 changed files with 10 additions and 1 deletions

View File

@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
public class ComponentClassLoader {
private static final Map<String, URLClassLoader> classLoaders = new HashMap<>();
@ -52,11 +53,19 @@ public class ComponentClassLoader {
return null;
}
return IOUtils.toString(is, StandardCharsets.UTF_8);
//多行只取第1行并处理空格
String[] lines = IOUtils.toString(is, StandardCharsets.UTF_8).split("\\s");
if (lines.length == 0) {
return null;
}
return lines[0].trim();
}
public static <T> T getComponent(String name, File jarFile) throws Exception {
String className = addUrl(name, jarFile);
if (StringUtils.isBlank(className)) {
throw new RuntimeException("component class does not exist");
}
Class<T> componentClass = findClass(name, className);
return componentClass.getDeclaredConstructor().newInstance();
}