Logo

Logo

利用java类加载以及反射-动态加载jar包实现热部署

Pastore Antonio
Pastore Antonio 2023年09月15日
304 阅读 0 评论 约 2266 字 阅读约 5 分钟

利用java类加载以及反射-动态加载jar包实现热部署

业务背景:
1.a.jar中的test方法依赖两个其他的jar包分别为b.jar和c.jar,类路径com.Product
2.a.jar中的test方法调用b.jar的test方法,使a产品加工成b产品,再调用c.jar中的test方法使b产品加工成c产品。由于线上环境活动频繁变动,所以使用类加载以及反射实现更新应用而不重启应用的热部署。
代码实现如下:

`
/**

  • This is the entrance of the test program. please do not modify the modifiers

  • of this class and methods. Please do not modify the input and output

  • parameters.

  • @author tom
    */
    public class Test {

    public static void main(String[] args) throws IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchMethodException, MalformedURLException, ClassNotFoundException {
    new Test().test(new File(“b.jar”),new File(“b.jar”),“helloWorld!”);
    }
    /**

    • By calling this function. Call the functions in the three jar packages and

    • return data.

    • @param jarFileB

    • @param jarFileC

    • @param text original string

    • @return final decrypted string
      */
      public String test( File jarFileB, File jarFileC, String text) throws MalformedURLException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
      URL urlB = new URL(“file:”+jarFileB.getPath());
      URLClassLoader classLoaderA = new URLClassLoader(new URL[]{urlB}, Thread.currentThread()
      .getContextClassLoader());
      Class<?> product = classLoaderA.loadClass(“com.Product”);
      //获取实例
      Object obj = product.newInstance();
      //获取方法
      Method method=product.getDeclaredMethod(“test”,String.class);
      //执行方法
      Object result1 = method.invoke(obj, text);

      Object result2 = execute(jarFileC, result1.toString());
      execute(new File(“a.jar”),result2.toString());

      // TODO According to the requirements , write the implementation code.
      return null;

    }

    private Object execute(File file,String text) throws InvocationTargetException, IllegalAccessException, NoSuchMethodException, InstantiationException, ClassNotFoundException, MalformedURLException {
    URL url1 = new URL(“file:”+file.getPath());
    URLClassLoader classLoaderA = new URLClassLoader(new URL[]{url1}, Thread.currentThread()
    .getContextClassLoader());
    Class<?> product = classLoaderA.loadClass(“com.Product”);
    //获取实例
    Object obj = product.newInstance();
    //获取方法
    Method method=product.getDeclaredMethod(“test”,String.class);
    //执行方法
    Object result = method.invoke(obj, text);
    return result;
    }

}`)

查看完整代码

橙子主题打折出售

其实我不卖,主要是这里是放广告的,所以就放了一个
毕竟主题都没做完,卖了也是坑.

购买它
部分文章可能存在转载,如果涉及到侵权,请联系删除文章。

探索AIGC相关的精彩内容,共 15 篇文章

Azure AI 服务之语音识别

简介 Azure AI 服务中的语音识别 API 是微软提供的一项先进技术,旨在帮助开发者轻松实现语 ... 利用java类加载以及反射-动态加载jar包实现热部署

2026-02-17 · Xzavier Aaron
MCP | 一文详解什么是 MCP以及 MCP 可以做什么

一、什么是 MCP MCP(Model Context Protocol)是一个专为大型语言模型(L ... 利用java类加载以及反射-动态加载jar包实现热部署

2026-02-14 · Shen, Luke
你的工作流程,值得一个“全自动数字分身”:录制、截图、成文,一气呵成

一、一句话认识 TestFlow Recorder 在数字化工作环境中,如何准确记录操作步骤并生成清 ... 利用java类加载以及反射-动态加载jar包实现热部署

2026-02-14 · Xzavier Aaron
Flowise 前端框架配置指南

用户需求 问题:有没有适合配置 Flowise 的前端框架? 目标:寻找类似 Open WebUI ... 利用java类加载以及反射-动态加载jar包实现热部署

2026-02-14 · Xzavier Aaron