Difference between AppClassloader and SystemClassloader

后端 未结 3 1819
一生所求
一生所求 2021-02-02 00:16

I am so confused about these two class loaders. When talking about the hierarchy of Java class loaders, usually the bootstrap classloader and ext class loader and the third one

3条回答
  •  借酒劲吻你
    2021-02-02 00:55

    The third in the class loader hierarchy is the SystemClassloader. It is also referred as ApplicationClassloader (or AppClassLoader) at some places. This loader loads the our application code and classes found in the classpath.

    Regarding the below method in the Classloader:

    public static ClassLoader getSystemClassLoader()

    Javadoc says:

    Returns the system class loader for delegation. This is the default delegation parent for new ClassLoader instances, and is typically the class loader used to start the application.

    The important piece here is

    This is the default delegation parent for new ClassLoader instances, and is typically the class loader used to start the application

    Which means, if we create our own Custom or new classloader in our application, the System or Application class loader becomes the parent for our Custom or new classloader. And calling the getSystemClassLoader() method in Custom or new Classloader returns the System(aka Application) classloader. This aligns with the java class loader delegation model as well.

    And the System (aka Application) class loader is the one which has loaded our class or app from classpath.

提交回复
热议问题