java ClassLoader

获取 ClassLoader 方式

// 上下文类加载器
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

// 当前类
classLoader = Main.class.getClassLoader();


// 系统
classLoader = ClassLoader.getSystemClassLoader();

http://blog.csdn.net/zhoudaxia/article/details/35897057html

老是使用上下文类加载器”或者“老是使用当前类加载器spring

通常来讲,上下文类加载器要比当前类加载器更适合于框架编程,而当前类加载器则更适合于业务逻辑编程。   编程

// spring 加载器
public static ClassLoader getDefaultClassLoader() {
	ClassLoader cl = null;
	try {
		cl = Thread.currentThread().getContextClassLoader();
	}
	catch (Throwable ex) {
		// Cannot access thread context ClassLoader - falling back...
	}
	if (cl == null) {
		// No thread context class loader -> use class loader of this class.
		cl = ClassUtils.class.getClassLoader();
		if (cl == null) {
			// getClassLoader() returning null indicates the bootstrap ClassLoader
			try {
				cl = ClassLoader.getSystemClassLoader();
			}
			catch (Throwable ex) {
				// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
			}
		}
	}
	return cl;
}

ClassLoader 加载类

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class c = classLoader.loadClass("Main");
Main main = (Main) c.newInstance();
main.show();

classLoader 加载配置文件

读取 src/main/resources/app.conf 配置文件bootstrap

try {
	Properties properties = new Properties();
    InputStream inputStream = Thread.currentThread()
        						.getContextClassLoader()
       							.getResourceAsStream("app.conf");
    properties.load(inputStream);

} catch (Exception e) {
    e.printStackTrace();
}

参考:app

http://blog.csdn.net/xyang81/article/details/7292380框架

http://blog.chinaunix.net/uid-21227800-id-65885.htmlui

http://blog.csdn.net/zhoutaohenan/article/details/8467271this

相关文章
相关标签/搜索