在运行时覆盖SpringBootJar类 [英] Override SpringBootJar classes at runtime

查看:111
本文介绍了在运行时覆盖SpringBootJar类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个用于创建SpringBoot微服务的系统.该微服务将通过 SystemAdapter 接口与外部系统通信.我们正在创建一个API,供人们构建自己的系统适配器.我正在寻找一种启动微服务的方法-在classPath中传递一个jar,并使其使用 SystemAdapater 的实现,而不是嵌入jar中的默认实现./p>

是否可以直接使用Gradle和 SpringBoot bootJar 任务来做到这一点.

我尝试采用的一种方法是:

  bootJar {排除'**/adapters/**'}任务saJar(类型:罐子){来自sourceSets.main.output包括'**/adapters/**'baseName'SA'}bootJar.dependsOn(saJar) 

我试图写出2个不同的Jars,但我没得到

java -cp SA.jar -jar microService.jar 可以正常工作

感谢您能提供的任何帮助.

解决方案

看看Spring Boot属性启动器,它将允许您干净地加载外部SystemAdapter实现.
启用PropertiesLauncher:
1.在主应用程序中,将应用程序布局设置为 ZIP

springBoot {mainClass ="..."布局=邮政编码"...}

    在实现捆绑包中的
  1. 将布局设置为 MODULE

springBoot {mainClass ="..."布局=模块"...}

  1. 添加以下cli指向外部jar所在的文件夹.

java -Dloader.path = file:/path/to/SystemAdaptery-implementations-folder-jar main-app.jar

这是用于加载实现的,您可以将其与spring boot的ServiceLoader(或简称为jdk服务加载器)结合使用,以注册SystemAdaptery实现实例并管理其调用.

I'm working with a system where we are creating a SpringBoot micro service. That micro service will communicate with external systems via a SystemAdapter interface. We are creating an API for people to build their own system adapters. What I'm looking for is a way to startup my micro-service - pass in a jar on the classPath and have it use that implementation of the SystemAdapater rather than the default one embedded in the jar.

Is there a straight forward to do this with Gradle and the bootJar task of SpringBoot.

One approach I tried to take was:

bootJar {
    exclude '**/adapters/**'
}

task saJar(type: Jar) {
    from sourceSets.main.output
    include '**/adapters/**'
    baseName 'SA'
}

bootJar.dependsOn(saJar)

I tried to write out 2 different Jars but I couldn't get

java -cp SA.jar -jar microService.jar to actually work

Thanks for any assistance that can be offered.

解决方案

Take a look on spring boot properties launcher, it will allow you to load the external SystemAdapter implementations cleanly.
To enable the PropertiesLauncher:
1. in the main app set the app layout to ZIP

springBoot{ mainClass = "..." layout = "ZIP" ... }

  1. in the implementation bundle set the layout to MODULE

springBoot{ mainClass = "..." layout = "MODULE" ... }

  1. Add the following cli arg to point to the folder where external jars are places.

java -Dloader.path=file:/path/to/SystemAdaptery-implementations-folder -jar main-app.jar

That was for loading the implementations, you can couple it with spring boot ServiceLoader (or simply the jdk service loader) to register the SystemAdaptery implementation instances and manage their invocations.

这篇关于在运行时覆盖SpringBootJar类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆