Logo

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

photo

2023年09月15日

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

}`)

橙子主题打折出售

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

购买它
所有附件
该文章没有附件.
本文为原创文章,请注意保留出处!

热门文章

修复群晖Synology Drive client右键菜单缺失问题 本教程主要解决windows10右键菜单中没有SynologyDrive菜单的问题,整体思路是找到...修复群晖SynologyDriveclient右键菜单缺失问题 作者:Pastore Antonio
2007 浏览量
docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 前言:有时候我们会在docker上下载一个镜像,或者是上传一个镜像到docker上,甚至有时候就是在...docker如何查看一个镜像内部的目录结构及其内部都有哪些文件 作者:Pastore Antonio
1944 浏览量
Adobe Acrobat Pro 激活 这里记录了一些AdobeAcrobat的激活教程和组件。浏览量:1,826 作者:Pastore Antonio
1669 浏览量
追寻日出,找回自己 为什么我要去追寻日出?其实我是一个很懒的人,每次都起不来,直到有一次我在租房中睡到了大天亮,阳光照...追寻日出,找回自己 作者:Pastore Antonio
1626 浏览量
Swagger2 接口多级分组方法 swagger无疑是Java开发的最佳伴侣,接口非常方便调试;当然也有用Postman,因人而异吧...Swagger2接口多级分组方法 作者:Pastore Antonio
1533 浏览量