Springboot 动态重载class文件

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值