gpt4 book ai didi

java - SpringBoot : Can I @Autowire Bean in runnable JAR from JAR provided using java -cp?

转载 作者:行者123 更新时间:2023-12-02 02:57:09 25 4
gpt4 key购买 nike

我有可运行的 JAR A,其中包含接口(interface):

interface FooInterface {
void foo();
...
}

在 JAR A 中,我还有一个类试图 Autowiring FooInterface 实现:

class Other{
@Autowired
FooInterface fooInterfaceImplementation;
...
}

在其他项目 B 中,我将 jar A 作为外部库和 FooInterface 的实现:

@Component
class BarClass implements FooInterface {
void foo(){...}
...
}

我正在尝试使用以下命令运行可运行的 A JAR,其中包含 JAR B 中的类:

java -jar A.jar -cp B.jar

但它以以下异常结束:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ocado.cfc.optimisation.AlgorithmInterface' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:348)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at com.ocado.cfc.optimisation.Executable.main(Executable.java:81)

是否可以以这种方式 Autowiring 所需的 bean?

非常感谢任何帮助。

最佳答案

如果您尝试同时使用-jar-cp,则不可能。使用 -jar 时,-cp 将被忽略。

如果您想在类路径上拥有多个 jar,您可以使用 -cp 传递它们。然后,您还必须提供要启动的主类的名称。鉴于您似乎正在使用 Spring Boot,这可能如下所示:

java -cp A.jar:B.jar org.springframework.boot.loader.JarLauncher

您可能还对 Spring Boot 的 PropertiesLauncher 感兴趣它允许您创建具有可配置类路径的可执行 jar。

关于java - SpringBoot : Can I @Autowire Bean in runnable JAR from JAR provided using java -cp?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42890479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com