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

}`)

橙子主题打折出售

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

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

热门文章

西游记之大圣归来 《西游记之大圣归来》是根据中国传统神话故事《西游记》进行拓展和演绎的3D动画电影。由横店影视、天空之城、燕城十月与微影时代作为出品方,高路动画、恭梓兄弟、世纪长龙、山东影视、东台龙行盛世、淮安西游产业与永康壹禾作为联合出品方出品,田晓鹏执导,张磊、林子杰、刘九容和童自荣等联袂配音。影片讲述了已于五行山下寂寞沉潜五百年的孙悟空被儿时的唐僧——俗名江流儿的小和尚误打误撞地解除了封印,在相互陪伴的冒险之旅中找回初心,完成自我救赎的故事。 作者:Pastore Antonio
1448 浏览量
常用的Centos操作Shell命令 玩WordPress的时候经常会接触到一些常用的命令,老是忘记……人年纪大了就是不给力啊。浏览量:...常用的Centos操作Shell命令 作者:Pastore Antonio
1387 浏览量
ffmpeg+srs 实现直播流 这篇文章是我在做直播流的时候收集的所有素材,没有太多的整理,都放到了脑袋里面了,之后有时间了再整理成...ffmpeg+srs实现直播流 作者:Pastore Antonio
1387 浏览量
项目十大管理和五大过程 PMBOK五大过程组是什么?PMBOK五大过程组是:启动过程、规划过程、执行过程、监...项目十大管理和五大过程 作者:Pastore Antonio
1363 浏览量
[ Office 365 开发系列 ] 身份认证 前言本文完全原创,转载请说明出处,希望对大家有用。通常我们在开发一个应用时,需要考虑用户身份认证...[Office365开发系列]身份认证 作者:Pastore Antonio
1357 浏览量