简体   繁体   English

GroovyClassLoader / ClassLoader如何工作?

[英]How does GroovyClassLoader/ClassLoader work?

I'm seeing code like 我看到的代码就像

GroovyClassLoader cLoader = new GroovyClassLoader(this.class.getClassLoader())

Followed by something like: 接下来是这样的:

cLoader.loadClass([class name])

I'm interested in what I should know about the GroovyClassLoader class and what the purpose of this.class.getClassLoader() is. 我对我应该了解的GroovyClassLoader类以及this.class.getClassLoader()的用途感兴趣。

Class loaders work in a vertical hierarchy manner, in fact in java there are three built in class loaders in this hierarchy: 类加载器以垂直层次结构方式工作,实际上在java中,此层次结构中有三个内置类加载器:

类加载器层次结构

So when you pass this.class.getClassLoader( ) to the constructor you are creating a classloader whose parent is the classloader which loaded the current class, which will give you this kind classloader hierarchy. 因此,当您将this.class.getClassLoader( )传递给构造函数时,您将创建一个类加载器,其父级是加载当前类的类加载器,它将为您提供这种类加载器层次结构。

在此输入图像描述

Why creating a classloader this way?, why not get the built-in ones? 为什么要这样创建一个类加载器?为什么不用内置的? that is upto you. 那取决于你。

But one fact to remind here is classloaders load classes in a top down fashion . 但是,这里要提醒的一个事实是类加载器以自上而下的方式加载类 A classloader asks its parent to load a class, if the parent cant find the class it loads the class by itself ( notice the call is recuring) and another fact is classloaders have a cache , loaded classes are cached for sometime. 类加载器要求其父级加载一个类,如果父级不能找到它自己加载类的类(注意调用是在进行复活),另一个事实是类加载器有一个缓存 ,加载的类会被缓存一段时间。

So I usually use Thread.currentThread.getClassLoader() ( which I believe is similar to urs) because this gives me the loader which loaded the current running thread and I believe it is near to my other classes and the hope is it might have cached the class I am requesting. 所以我通常使用Thread.currentThread.getClassLoader() (我相信它类似于urs),因为这给了我加载当前正在运行的线程的加载器,我相信它接近我的其他类,希望它可能已经缓存我要求的课程。

As per documentation, this.class.ClassLoader() parameter passed to the constructor is then taken as the created GroovyClassLoader 's parent (instead of using the current Thread's context Class loader as parent - the default behavior). 根据文档,传递给构造函数的this.class.ClassLoader()参数然后被视为创建的GroovyClassLoader的父级(而不是使用当前Thread的上下文类加载器作为父级 - 默认行为)。

I'm no expert in class loading, but AFAIK the classloader's parent is first called in search for a given class. 我不是类加载的专家,但AFAIK首先调用类加载器的父级来搜索给定的类。

As for what should be known about it, I can't tell you any more than is available in the documentation. 至于应该知道什么,我不能告诉你任何文件中提供的内容。

Groovy is a scripting language and as such, you'll find yourself in a lot of places where you're loading a Groovy script from a file and want to execute it. Groovy是一种脚本语言,因此,您会发现自己处于从文件加载Groovy脚本并想要执行它的很多地方。 There are two problems here: 这里有两个问题:

  • Groovy code needs a suitable environment Groovy代码需要一个合适的环境
  • You eventually want to get rid of the script (and all its classes) 你最终希望摆脱脚本(及其所有类)

The GroovyClassLoader will make sure that scripts (which are loaded while it's active) will work. GroovyClassLoader将确保脚本(在它处于活动状态时加载)将起作用。 And when it is garbage collected, it will make sure that all resources can be GC'd as well (otherwise, you would eventually run into problems if the script on disk changed and you wanted to reload it or you would run out memory, etc.) 当它被垃圾收集时,它将确保所有资源也可以GC(否则,如果磁盘上的脚本发生更改并且您想重新加载它或者您将耗尽内存等,最终会遇到问题) 。)

Groovy code needs access to normal Java classes, that's why you have to give it a parent classloader. Groovy代码需要访问普通的Java类,这就是为什么你必须给它一个父类加载器。 The Groovy classloader will ask the parent for anything that it doesn't know itself. Groovy类加载器将向父级询问它自己不知道的任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM