使用ClassLoader加载类

使用classLoader手动加载指定的类:

//拿到当前线程上下文ClassLoader实例,是AppClassLoader的实例
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
//调用loaderClass方法,把Student类加载到内存   
Class<Student> clazz = (Class<Student>)classLoader.loadClass("com.test.bean.Student");
//生成一个实例
Student student = clazz.newInstance();
System.out.println(student);  
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {  
e.printStackTrace();
} catch (IllegalAccessException e) {  
e.printStackTrace();
}




你可能感兴趣的:(使用ClassLoader加载类)