OSGi类加载机制

本文对比分析了Java类加载技术的传统使用方式与OSGi技术在类加载方面的优势,包括类路径管理、类加载器层级关系及组件隔离等关键特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

0. Java’s Virtual Machine, Java Classes, and the CLASSPATH Variable

    Class文件:由字节码组成,字节码文件运行在JVM上;默认情况下是由最后声明、最后加载的

    ClassLoader加载。

    ClassPath: 定位Class文件的位置。

1. Classloader Technology

    类加载技术是基于分层的机制来处理Class文件的,Java集成了三种类型的ClassLoader:

    ■ Bootstrap classloader —Loads classes from the core Java libraries present in the lib

       directory of your java home directory.
    ■ Extension(Sytem) classloader —Loads classes from the lib/ext directory of your Java

       platform or any directory specified in the java.ext.dirs system property.
    ■ Application classloader —Loads classes based on the elements specified in the

       application classpath.

1.1 Traditional use of classloaders in Java

    These three classloaders are linked in a hierarchical relationship. In order to load a class,

    a classloader first asks its parent if it’s able to load the class. If not,it tries to load the class

    itself. This mechanism is used at each classloader level. In this situation,the same

    classloaders are used to resolve all the classes of an application, which makes it difficult

    to isolate the resolution of different classes.

1.2 The classloader inOSGi

    OSGi technology provides a different approach to using classloaders; it’s not based on a

    hierarchical approach but on the concept of classloader chaining(这些ClassLoader是OSGi

    管理的), which allows fined-graine(细粒度) control of the visibility of classes from each other.

    In this context, each component is associated with a dedicated classloader. According to

    the component configuration, this  classloader is linked to other components’ classloaders

    for the resolution of classes outside  the current component. Having classloaders

    dedicated to components makes isolation between  components possible, and, by default,

    no class can be seen outside a component. 

    Instead, you need to explicitly import and export them by configuring the corresponding

    packages in the manifest files.

2. JSR 294—Java module system
    JSR 294 aims to provide a static module system inside Java itself. The JSR is based on a

    similar approach to the OSGi Require-Bundle header but with a greater emphasis on

    language support and the runtime modularity of the JDK itself. JSR 294 doesn’t have the

    same support as OSGi to make different versions of the same class cohabit in the same

    process.

3. OSGi类加载的优点

    (1) Bundle之间可直接共享类,不需要将一个JAR文件提升为一个Parent Class Loader.

    (2) 同一时间可部署同一个类的不同版本,而不冲突。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值