1.定义重载接口
@Autowired
Excuteplugins excuteplugins;
@ResponseBody
@RequestMapping(value = "/reloadClass", method = RequestMethod.GET)
public void reloadClass() throws MalformedURLException {
File file;
List<URL> urlList = new ArrayList();
String pathTemp = "target/CLASS";
if ((file = new File(pathTemp)).exists()) {
urlList.add(file.toURI().toURL());
}
URL[] urlArray = new URL[urlList.size()];
for (int i = 0; i < urlList.size(); i++) {
urlArray[i] = urlList.get(i);
}
URLClassLoader classLoader = new URLClassLoader(urlArray, Thread.currentThread().getContextClassLoader());
excuteplugins.myClassLoader = classLoader;
}
2.使用class方法调用
public class Excuteplugins implements IExcute {
private URLClassLoader myClassLoader;
@Override
public ResultBean excute(String plugInName, Class[] argTypes, Object[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException, ParseException, java.text.ParseException, SQLException, IOException {
ResultBean resultBean = new ResultBean();
if (myClassLoader == null) {
resultBean.fillResult(ResultCode.ERROR);
resultBean.setMessage("插件class未加载!");
return resultBean;
}
List<IcsCheckReport> lstResult = null;
//class插件
Class<?> clz = myClassLoader.loadClass(plugInName.trim());
lstResult = (List<IcsCheckReport>) runMethord(clz, "excute", argTypes, args);
lstResult.forEach(r -> r.setFeatureWkt(null));
// geomSet(lstResult);
resultBean.setData(lstResult);
resultBean.fillResult(ResultCode.SUCCESS);
return resultBean;
}
}