我有一个在程序执行期间需要多次使用的类列表,我需要动态加载它们,因为它们的代码可能会经常更改,我想将它们视为服务,以便我可以轻松更新它们。这是我的问题:
即使没有更改,UrlClassLoader 是否总是再次加载该类?这会导致性能或内存问题吗?
这是我的测试代码:
package classloader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
public class ClassLoaderClass {
public static void main(String[] args) throws ClassNotFoundException, MalformedURLException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, InterruptedException {
//no paramater
Class noparams[] = {};
URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{new URL("file:///C:/Users/hm/Documents/classes/")});
Class gsonClass = urlClassLoader.loadClass("AppTest");
Constructor constructor = gsonClass.getConstructor();
Object Apptest = constructor.newInstance();
Method method = gsonClass.getMethod("printIt",noparams);
Object returnObj = method.invoke(Apptest, null);
String jsonString = (String)returnObj;
}
}
我希望你能帮助我,谢谢。