网站架构技术,自己买个服务器有什么用,app推广策略,专业的专业的网页制作公司为了保证兼容性#xff0c;JDK9没有从根本上改变三层类加载器的架构和双亲委派模型#xff0c;但为了模块化系统的顺利运行#xff0c;仍然发生了一些值得被注意的变动。
一、变动1
由于引入了模块化概念#xff0c;所以不同的类加载器回去加载属于不同模块的类
启动类加…为了保证兼容性JDK9没有从根本上改变三层类加载器的架构和双亲委派模型但为了模块化系统的顺利运行仍然发生了一些值得被注意的变动。
一、变动1
由于引入了模块化概念所以不同的类加载器回去加载属于不同模块的类
启动类加载器 平台类加载器 应用类加载器 二、变动二
扩展机制被移除但是扩展类加载器由于向后兼容性的原因被保留然后被重命名为平台类加载器Platform ClassLoader可以通过ClassLoader的新增方法getPlatformClassLoader来获取。
原因 JDK9是基于模块化进行构建的它将原来的rt.jar和tools.jar拆分成了很多的JMOD文件文件中的Java类库已经天然地满足了可扩展的需求所以扩展机制已经没有继续存在的价值了。
public class ClassLoaderTest {public static void main(String[] args) {System.out.println(ClassLoaderTest.class.getClassLoader());System.out.println(ClassLoaderTest.class.getClassLoader().getParent());System.out.println(ClassLoaderTest.class.getClassLoader().getParent().getParent());}
}三、变动三
平台类加载器和应用程序类加载器都不再继承于java.net.URLClassLoader而是继承于jdk.internal.loader.BuiltinClassLoader并且连启动类加载器都是。 四、变动四
类加载器有了名称属性该名称可以在构造方法中进行指定通过getName方法来获取。应用类加载器的默认名称是app平台类加载器的默认名称是platform。
五、变动五
启动类加载器现在是由JVM内部和Java类库共同协作实现的类加载器但为了与之前代码进行兼容在尝试获取启动类加载器时仍然会返回null而不会得到对应的BootClassLoader实例。
六、变动六
类加载的委派关系发生了变动。 对比 在JDK9之前的版本中当扩展及应用程序类加载器收到类加载请求会委派给父加载器加载只有当父加载器反馈自己无法完成这个加载请求时才会由子类加载器尝试进行加载。在JDK9之后的版本中当平台及应用程序类加载器收到类加载请求在委派给父加载器加载前要先判断该类是否能够归属到某一个系统模块中如果可以找到这样的归属关系就要优先委派给负责那个模块的加载器完成加载。 七、测试代码
public class DemoApplication {public static void main(String[] args) {System.out.println(DemoApplication.class.getClassLoader());System.out.println(DemoApplication.class.getClassLoader().getParent());System.out.println(DemoApplication.class.getClassLoader().getParent().getParent());// 获取系统类加载器System.out.println(ClassLoader.getSystemClassLoader());// 获取平台类加载器System.out.println(ClassLoader.getPlatformClassLoader());// 获取类的加载器的名称System.out.println(DemoApplication.class.getClassLoader().getName());System.out.println(DemoApplication.class.getClassLoader().getParent().getName());ModuleLayer layer ModuleLayer.boot();SetModule allModules layer.modules();//打印所有已加载模块及其对应的类加载器for (Module module : allModules) {ClassLoader classLoader module.getClassLoader();ModuleDescriptor descriptor module.getDescriptor();System.out.println(Module: descriptor.name() , Class Loader: classLoader);}}
}