栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot 动态重载class文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
        }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/720521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号