java接口工厂

来源:互联网 发布:苹果电脑软件少 编辑:程序博客网 时间:2024/06/07 18:15
/**
* 创建接口
* 根据字符串类型的类的全路径(包含包名称),创建类的实例,最后强转为接口

* @param className = "com.quekua.iSpider.handle.impl.YixunHandleImpl";
* @param fileName = "E:/workspace/quekuawang/Developer/Shooter/iSpider/libs/handleImpl.jar"; 
* @return 
* IHandle
* @exception/throws
*/
public static IHandlecreate(String className, String fileName){
IHandle iHandle = null;
try {
       File file = new File(fileName);
ArrayList<URL> classPath = new ArrayList<URL>();
classPath.add(file.toURI().toURL());
//URL[] arrayUrl = new URL[0];
ClassLoader loader = new URLClassLoader(classPath.toArray(new URL[0]));
       //Thread.currentThread().setContextClassLoader(loader);
       Class<?> mainClass = Class.forName(className, true, loader);
       iHandle = (IHandle)mainClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return iHandle;
}
0 0