1 手动加载一个类


klass = urlClassLoader.loadClass(“io.study.service.HelloService”);;)
手动用反射实例化 或者接口;)

2 当前类的类加载器是哪个?;)如何破坏类加载的传递


当前类的类加载器的loadclass委托其他类记载器loadclass;)

3 biz与;)plugin 可以隔离,也可以自行加载



com.alipay.sofa.SampleClass1
com.alipay.sofa.SampleClass2



com.alipay.sofa
org.springframework.*

4 为什么biz加载类是bizclassloader

1 com.alipay.sofa.ark.container.model.BizModel#start设置当前线程的classloader为bizclassloader (每个类biz都有一个成员bizclassloader)

  1. public void start(String[] args) throws Throwable {
  2. AssertUtils.isTrue(bizState == BizState.RESOLVED, "BizState must be RESOLVED");
  3. if (mainClass == null) {
  4. thrownew ArkRuntimeException(String.format("biz: %s has no main method", getBizName()));
  5. }
  6. ClassLoader oldClassLoader = ClassLoaderUtils.pushContextClassLoader(this.classLoader);
  7. EventAdminService eventAdminService = ArkServiceContainerHolder.getContainer().getService(
  8. EventAdminService.class);
  9. try {
  10. eventAdminService.sendEvent(new BeforeBizStartupEvent(this));
  11. resetProperties();
  12. MainMethodRunner mainMethodRunner = new MainMethodRunner(mainClass, args);
  13. mainMethodRunner.run();
  14. // this can trigger health checker handler
  15. eventAdminService.sendEvent(new AfterBizStartupEvent(this));
  16. } catch (Throwable e) {
  17. bizState = BizState.BROKEN;
  18. throw e;
  19. } finally {
  20. ClassLoaderUtils.popContextClassLoader(oldClassLoader);
  21. }
  22. BizManagerService bizManagerService = ArkServiceContainerHolder.getContainer().getService(
  23. BizManagerService.class);
  24. if (bizManagerService.getActiveBiz(bizName) == null) {
  25. bizState = BizState.ACTIVATED;
  26. } else {
  27. bizState = BizState.DEACTIVATED;
  28. }
  29. }

2 在com.alipay.sofa.ark.bootstrap.MainMethodRunner#run 中使用Thread.currentThread().getContextClassLoader() 并反射调用

  1. public Object run() throws Exception {
  2. Class<?> mainClass = Thread.currentThread().getContextClassLoader()
  3. .loadClass(this.mainClassName);
  4. Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
  5. return mainMethod.invoke(null, new Object[] { this.args });
  6. }

5 biz加载器与plu加载器如何联通?

  1. <exported>
  2. <classes>
  3. <class>io.study.plugin.ArkPlugin1Service</class>
  4. </classes>
  5. </exported>

biz加载器通过这个插件的exported找到对应的插件类加载器,在exportClassAndClassLoaderMap 已经存了对应关系。
for (String exportIndex : plugin.getExportClasses()) {
exportClassAndClassLoaderMap
.putIfAbsent(exportIndex, plugin.getPluginClassLoader());
}