Springboot 动态重载class文件

Springboot 动态重载class文件,第1张

Springboot 动态重载class文件

1.定义重载接口

    @Autowired
    Excuteplugins excuteplugins;
    @ResponseBody
    @RequestMapping(value = "/reloadClass", method = RequestMethod.GET)
    public void reloadClass() throws MalformedURLException {
        File file;
        List 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 lstResult = null;

            //class插件
            Class clz = myClassLoader.loadClass(plugInName.trim());
            lstResult = (List) runMethord(clz, "excute", argTypes, args);
            lstResult.forEach(r -> r.setFeatureWkt(null));
            // geomSet(lstResult);
            resultBean.setData(lstResult);
            resultBean.fillResult(ResultCode.SUCCESS);
            return resultBean;
        }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5716835.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存