自定义类加载器(ClassLoader + URLClassLoader)

来源:互联网 发布:淘宝偏袒买家 编辑:程序博客网 时间:2024/04/30 18:55

【0】README

0.1)本文主要对类加载器进行分析,且 URLClassLoader是 ClassLoader的子类;

0.2)关于如何设置类加载器的加载路径,参见 对servlet容器的补充

【1】URLClassLoader类加载器

1.1)URLClassLoader的一种加载方式



package com.tomcat.classloader;import java.io.File;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.net.URL;import java.net.URLClassLoader;public class URLClassLoaderTest {public final static String dir = System.getProperty("user.dir") + File.separator + "src" + File.separator;public static void main(String[] args) throws Exception {// Getting the jar URL which contains target classURL[] classLoaderUrls = new URL[]{new URL("file:" + dir)};// Create a new URLClassLoader URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);// Load the target class        Class<?> beanClass = urlClassLoader.loadClass("com.tomcat.test.Bean");                // Create a new instance from the loaded class        Constructor<?> constructor = beanClass.getConstructor();        Object beanObj = constructor.newInstance();                // Getting a method from the loaded class and invoke it        Method method = beanClass.getMethod("sayHello");        method.invoke(beanObj);}}
package com.tomcat.test;public class Bean {public void sayHello() {System.out.println("Hello from loaded Bean class for test!!!");}}


1.2)URLClassLoader的另一种加载方式

package com.tomcat.classloader;import java.io.File;import java.lang.reflect.Constructor;import java.lang.reflect.Method;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;public class URLClassLoaderTest {public final static String dir = System.getProperty("user.dir") + File.separator + "webroot" + File.separator;public static void main(String[] args) {try {File file = new File(dir);URL url = file.toURI().toURL(); // attend this lineURL[] urls = new URL[]{url};URLClassLoader loader = new URLClassLoader(urls, null, null);  System.out.println("test = " + loader.getURLs()[0].toString());Class<?> beanClass = loader.loadClass("servlet.Bean");Object obj = beanClass.newInstance();Method method = beanClass.getMethod("sayHello");method.invoke(obj);} catch (Exception e) {e.printStackTrace();}  }}


【2】ClassLoader类加载器的加载方式

package com.tomcat.test;import java.lang.reflect.Constructor;import java.lang.reflect.Method;public class ClassLoaderTest {    // 第一种加载类的方式    public static void main1(String[] args) throws Exception {             Class<?> newClass = Class.forName("com.tomcat.classloader.Bean");         Constructor constructor = newClass.getConstructor();         Object obj = constructor.newInstance();         Method method = newClass.getMethod("sayHello");         method.invoke(obj);    }        // 第二种加载类的方式    public static void main(String[] args) throws Exception {            ClassLoader loader = Thread.currentThread().getContextClassLoader();        Class<?> newClass = loader.loadClass("com.tomcat.classloader.Bean");        Constructor constructor = newClass.getConstructor();        Object obj = constructor.newInstance();        Method method = newClass.getMethod("sayHello");        method.invoke(obj);   }}    


0 0