Eclipse书籍部分翻译(英译中)10

来源:百度知道 编辑:UC知道 时间:2024/09/14 13:42:48
Issues with Context Classloaders
Since Java 1.2, the Class.forName(String) mechanism has been largely superseded by context classloading. As such, most modern class libraries use a context classloader. In the discussion below, we show how Eclipse transparently converts the use of context classloaders into something equivalent to Class.forName (String). Doing this allows the buddy loading and Dynamic-Import mechanisms described above to be used to eliminate ClassNotFoundExceptions and NoClassDefFoundErrors.
Each Java Thread has an associated context classloader field that contains a classloader. The classloader in this field is set, typically by the application container, to match the context of this current execution. That is, the field contains a classloader that has access to the classes related to the current execution (e.g., Web request being processed). Libraries such as log4j access and use the context classloader with the updated AppenderHelper code pattern below:

问题的背景下Classloaders
由于爪哇1.2 , Class.forName (弦乐)的机制已基本取代了背景classloading 。因此,最现代化的类库使用类加载器的范畴。在下文的讨论,我们将展示如何转换Eclipse的透明的使用范围classloaders成相当于Class.forName (字符串) 。这样做可以让好友装货和动态的进口机制如上所述,用来消除ClassNotFoundExceptions和NoClassDefFoundErrors 。
每个Java线程有相关的背景类加载器领域,其中包含类加载器。在类加载器在这方面设置,一般是通过集装箱的应用,以符合这一背景下当前执行。也就是说,外地包含一个类加载器已获得有关课程到目前执行的(例如, Web请求正在处理中) 。图书馆如log4j访问和使用类加载器的背景与更新AppenderHelper代码模式如下:

默认情况下,背景类装载器将是正常的Java应用类加载器。这就是,使用类加载器的范围内正常的Java应用情景等于使用Class.forName (弦乐)和只有一个类加载器,应用类加载器。当运行在Eclipse的,但代码模式上文所述失败,因为:
•默认情况下, Eclipse的不协商的应用类加载器。基于Eclipse的应用程序将其代码的动态插件classpaths ,而不是正常的Java应用程序类路径。
• Eclipse的无法检测插件上下文切换,并设置背景类装载器的要求。也就是说,没有办法告诉当执行方面的变化从一个插件下的工作是在Web应用服务器。
这些特点,再加上成分性质的Eclipse ,意味着价值的领域范畴类加载器,很少有用的。
客户还可以,但明确规定的范围类加载器之前,要求图书馆,使用类加载器的范畴。片段所示的一个例子,呼吁log4j使用此方法:
一是目前的情况下类装载器被保存。背景类加载器,然后设置为一个适当的值为目前的执行和log4j被称为。 log4j的AppenderHelper使用类加载器的背景,所以在这种情况下,它使用客户端的类加载器(例如, this.getClass ( ) 。 getClassLoader ( ) ) 。当手术完成后,