1 手动加载一个类
klass = urlClassLoader.loadClass(“io.study.service.HelloService”);;)
手动用反射实例化 或者接口;)
2 当前类的类加载器是哪个?;)如何破坏类加载的传递
当前类的类加载器的loadclass委托其他类记载器loadclass;)
3 biz与;)plugin 可以隔离,也可以自行加载
4 为什么biz加载类是bizclassloader
1 com.alipay.sofa.ark.container.model.BizModel#start设置当前线程的classloader为bizclassloader (每个类biz都有一个成员bizclassloader)
public void start(String[] args) throws Throwable {
AssertUtils.isTrue(bizState == BizState.RESOLVED, "BizState must be RESOLVED");
if (mainClass == null) {
thrownew ArkRuntimeException(String.format("biz: %s has no main method", getBizName()));
}
ClassLoader oldClassLoader = ClassLoaderUtils.pushContextClassLoader(this.classLoader);
EventAdminService eventAdminService = ArkServiceContainerHolder.getContainer().getService(
EventAdminService.class);
try {
eventAdminService.sendEvent(new BeforeBizStartupEvent(this));
resetProperties();
MainMethodRunner mainMethodRunner = new MainMethodRunner(mainClass, args);
mainMethodRunner.run();
// this can trigger health checker handler
eventAdminService.sendEvent(new AfterBizStartupEvent(this));
} catch (Throwable e) {
bizState = BizState.BROKEN;
throw e;
} finally {
ClassLoaderUtils.popContextClassLoader(oldClassLoader);
}
BizManagerService bizManagerService = ArkServiceContainerHolder.getContainer().getService(
BizManagerService.class);
if (bizManagerService.getActiveBiz(bizName) == null) {
bizState = BizState.ACTIVATED;
} else {
bizState = BizState.DEACTIVATED;
}
}
2 在com.alipay.sofa.ark.bootstrap.MainMethodRunner#run 中使用Thread.currentThread().getContextClassLoader() 并反射调用
public Object run() throws Exception {
Class<?> mainClass = Thread.currentThread().getContextClassLoader()
.loadClass(this.mainClassName);
Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
return mainMethod.invoke(null, new Object[] { this.args });
}
5 biz加载器与plu加载器如何联通?
<exported>
<classes>
<class>io.study.plugin.ArkPlugin1Service</class>
</classes>
</exported>
biz加载器通过这个插件的exported找到对应的插件类加载器,在exportClassAndClassLoaderMap 已经存了对应关系。
for (String exportIndex : plugin.getExportClasses()) {
exportClassAndClassLoaderMap
.putIfAbsent(exportIndex, plugin.getPluginClassLoader());
}