集册 Java实例教程 返回要使用的默认ClassLoader:通常是线程上下文ClassLoader(如果有);否则,返回默认值。加载ClassUtils类的ClassLoader将用作后备。

返回要使用的默认ClassLoader:通常是线程上下文ClassLoader(如果有);否则,返回默认值。加载ClassUtils类的ClassLoader将用作后备。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

641
返回要使用的默认ClassLoader:通常是线程上下文ClassLoader(如果有);否则,返回默认值。加载ClassUtils类的ClassLoader将用作后备。
// 来自 n o w j a v a . c o m - 时代Java



public class Main{

    public static void main(String[] argv) throws Exception{

        System.out.println(getDefaultClassLoader());

    }

    /**

     * Return the default ClassLoader to use: typically the thread context

     * ClassLoader, if available; the ClassLoader that loaded the ClassUtils

     * class will be used as fallback.

     * <p>Call this method if you intend to use the thread context ClassLoader

     * in a scenario where you absolutely need a non-null ClassLoader reference:

     * for example, for class path resource loading (but not necessarily for

     * <code>Class.forName</code>, which accepts a <code>null</code> ClassLoader

     * reference as well).

     * @return the default ClassLoader (never <code>null</code>)

     * @see java.lang.Thread#getContextClassLoader()

     */

    public static ClassLoader getDefaultClassLoader() {

        ClassLoader cl = null;

        try {

            cl = Thread.currentThread().getContextClassLoader();

        } catch (Throwable ex) {

            // Cannot access thread context ClassLoader - falling back to system class loader...

        }

        if (cl == null
展开阅读全文