简体   繁体   English

在 spring boot 中加载外部 jar

[英]Loading external jars in spring boot

How do we load additional jar at runtime along with boot jar.我们如何在运行时加载额外的 jar 和引导 jar。

Primary jar : Main .jar主 jarMain .jar

Additional jar : Support .jar附加 jar : Support .jar

Main project is a gradle boot project. Main项目是一个 gradle boot 项目。

Support project is NOT a gradle project but is given compile time dependencies to the required jars. Support项目不是 gradle 项目,而是为所需的 jar 提供编译时依赖性。

Contents of Support project:扶持项目内容:

@RestController
@RequestMapping("/test")
public class CustomService implements WebMvcConfigurer {


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public @ResponseBody String get() {
        return "Done!!";
    }
}

What i tried:我试过的:

java -cp Support.jar:Main.jar -Dloader.path=Support.jar -Xbootclasspath/p:alpn-boot-8.1.11.v20170118.jar -Dloader.main=com.abc.app.MyApplication  org.springframework.boot.loader.PropertiesLauncher

The boot starts up fine but the endpoint is not registered.引导启动正常,但端点未注册。

NOTE: I had mentioned annotation scanning.注意:我提到过注释扫描。

@SpringBootApplication
@ComponentScan("com.abc")
public class MyApplication {
   ....
}

Also the Main.jar will be run from various places by various users.此外,Main.jar 将由不同的用户从不同的地方运行。 Each user might provide his own version of Support.jar.每个用户都可以提供他自己的 Support.jar 版本。 So, hardcoding the dependency into the gradle file of Main project is not feasible.因此,将依赖项硬编码到 Main 项目的 gradle 文件中是不可行的。

Try adding @ComponentScan(basePackages=full.name.of.customservice.package ) to your spring application configuration, or make CustomService the same package as your @SpringApplication class尝试将@ComponentScan(basePackages=full.name.of.customservice.package ) 添加到您的 spring 应用程序配置中,或者使CustomService成为与您的@SpringApplication类相同的包

try using this - org.xeustechnologies.jcl.JarClassLoader from https://github.com/kamranzafar/JCL尝试使用这个 - 来自https://github.com/kamranzafar/JCL的 org.xeustechnologies.jcl.JarClassLoader

JCL is a configurable, dynamic and extensible custom classloader that loads java classes directly from Jar files and other sources. JCL 是一个可配置的、动态的和可扩展的自定义类加载器,它直接从 Jar 文件和其他来源加载 java 类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM