在运行时动态将jar添加到SpringBoot [英] Dynamically add jars to SpringBoot at runtime

查看:1388
本文介绍了在运行时动态将jar添加到SpringBoot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SpringBoot 1.4.2.我希望能够从命令行启动SpringBoot应用程序,并指定其他外部jar,在我启动该应用程序时也应对其进行组件扫描.我不能使用spring-boot-maven-plugin,因为当我要启动它时,我将没有它的spring boot应用程序的pom.

I'm using SpringBoot 1.4.2. I want to be able to launch my SpringBoot application from the command line, specifying additional, external jars that should also be component scanned at the time I launch the application. I can't use the spring-boot-maven-plugin because I won't have the spring boot application's pom when I want to launch it.

到目前为止,我已经尝试了以下操作,其中my-jar.jar是spring boot应用程序,而external-jar.jar是我想在命令行中包含的外部jar:

I have tried the following so far, where my-jar.jar is the spring boot application and external-jar.jar is the external jar I want to include on the command line:

java -cp my-jar.jar -Dloader.path=external-jar.jar -Dloader.main=com.my.Application org.springframework.boot.loader.JarLauncher

这将启动,但不进行组件扫描external-jar.jar

This launches but does not component scan external-jar.jar

java -cp external-jar.jar:my-jar.jar -Dloader.main=com.my.Application org.springframework.boot.loader.JarLauncher

这无法启动,表示external-jar.jar中的@Autowired条件未得到满足,即使my-jar.jar中的类确实满足了它们.我尝试过反转罐子在类路径上的顺序,但这不能解决问题.

This fails to launch, saying @Autowired conditions in external-jar.jar were not satisfied, even though classes in my-jar.jar do satisfy them. I have tried reversing the order of the jars on the classpath, this doesn't fix the issue.

java -Dloader.path="external-jar.jar" -jar my-jar.jar

这将启动,但不进行组件扫描external-jar.jar

This launches but does not component scan external-jar.jar

我在SpringApplication类顶部的组件扫描路径具有my-jar.jarexternal-jar.jar的程序包路径,并且我已经验证了直接在我的pom中引用external-jar.jar时会对其进行组件扫描.

My component scanning path at the top of my SpringApplication class has a package paths for both my-jar.jar and external-jar.jar and I have verified that when referencing external-jar.jar directly within my pom it is component scanned.

推荐答案

您使用了错误的启动器. PropertiesLauncher提供了对loader.pathloader.main的支持,但是您正在使用JarLauncher.

You're using the wrong launcher. Support for loader.path and loader.main is provided by PropertiesLauncher but you're using JarLauncher.

这篇关于在运行时动态将jar添加到SpringBoot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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