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

}`)

橙子主题打折出售

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

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

热门文章

WordPress 后台编辑器样式实现直接预览 在WordPress3.0以后,有一个新的实用功能:你可以更改默认后台编辑器(TinyMCE)的样...WordPress后台编辑器样式实现直接预览 作者:Pastore Antonio
1533 浏览量
【干货】Chrome插件(扩展)开发全攻略 写在前面我花了将近一个多月的时间断断续续写下这篇博文,并精心写下完整demo,写博客的辛苦大家懂的...【干货】Chrome插件(扩展)开发全攻略 作者:Pastore Antonio
1488 浏览量
memcached 处理 多端口:https://blog.csdn.net/Erica_1230/article/deta...memcached处理 作者:Pastore Antonio
1481 浏览量
使用Nginx+WordPress搭建个人网站 背景很多研究技术的朋友喜欢写博客。如果希望搭建一个完全属于自己的网站,也并不困难。这里简要分享一下...使用Nginx+WordPress搭建个人网站 作者:Pastore Antonio
1453 浏览量
C#图片处理 通常对一幅图片的处理包括:格式变换,缩放(Scale),翻转(Rotate),截取(Clip),滤镜...C#图片处理 作者:Pastore Antonio
1445 浏览量