Skip to content

Commit

Permalink
完成模块加载操作
Browse files Browse the repository at this point in the history
  • Loading branch information
Suomm committed Mar 17, 2021
1 parent c69123a commit 17f5afb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ public final class ModuleLoader {
private ModuleLoader() {
}

/**
* <p>被加载进入 JVM 的模块。可用以下代码,通过反射加载对象。
* <pre>
* Class&lt;?&gt; aClass = moduleLayer.findLoader("module name").loadClass("class name");
* </pre>
* <p><b>特别注意:调用该变量之前必须调用 {@link #load(Collection, Collection)} 方法!</b>
*/
public static ModuleLayer moduleLayer;

/**
* 根据模块名称,加载模块。
*
Expand All @@ -46,12 +55,14 @@ private ModuleLoader() {
* @exception FindException 模块加载失败
*/
public static Map<String, Property> load(Collection<String> basePath, Collection<String> names) {
// 将文件夹中的模块添加到运行环境中
ModuleFinder finder = ModuleFinder.of(basePath.stream().map(Path::of).toArray(Path[]::new));
ModuleLayer parent = ModuleLayer.boot();
Configuration cf = parent.configuration().resolve(finder, ModuleFinder.of(), names);
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithOneLoader(cf, scl);
return layer.modules()
moduleLayer = parent.defineModulesWithOneLoader(cf, scl);
// 加载模块的注解信息存入集合中
return moduleLayer.modules()
.stream()
.collect(Collectors.toMap(Module::getName, e -> e.getAnnotation(Property.class)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,15 @@ public class YamlLoader {
public Settings settings() {
try (BufferedReader br = new BufferedReader(
new FileReader("conf/settings.yml"))) {
// 根据 settings.yml 文件创建实体类
Settings settings = new Yaml().loadAs(br, Settings.class);
// 配置文件里没有任何内容
if (settings == null)
settings = new Settings();
// 初始化默认设置
return settings.init();
} catch (IOException e) {
// 文件不存在直接返回默认设置
return new Settings().init();
}
}
Expand Down

0 comments on commit 17f5afb

Please sign in to comment.