在运行时动态将jar添加到SpringBoot [英] Dynamically add jars to SpringBoot at runtime
问题描述
我正在使用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.jar
和external-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.path
和loader.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屋!