繁体   English   中英

在 Spring 引导加载外部 Jar

[英]Load External Jar in Spring Boot

我有一个 jar 文件,其中包含这个 class:

@Configuration
public class ExternalConfiguration {

  @Bean(name = "tab")
  public Tab getTab() {
    return new Tab();
  }
}

我的 Spring 引导应用程序如下所示:

@EnableAutoConfiguration
@PropertySource("classpath:application.properties")
@ComponentScan("com.xxx")
public class Application implements CommandLineRunner {

  @Autowired
  private ApplicationContext context;

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

  @Override
  public void run(String... arg0) throws Exception {
    Object o1 = context.getBean("tab");
  }

}

我在跑步时得到这个:

A component required a bean named 'tab' that could not be found.

我已经尝试了我能想到的 loader.path 的每一种组合:

loader.path=/opt/lib
loader.path=/opt/lib/
loader.path=/opt/lib/external.jar

我将它们放在 application.properties 中并在 loader.properties 中进行了尝试。 我还可以确认 ExternalConfiguration 的 package 属于 com.xxx——所以应该扫描它的组件是否正确?

我究竟做错了什么? 如何加载外部 jar 并获取 Spring 以正常加载它。 我不想将它添加到我的 pom.xml。
编辑当我将它添加到我的 pom 中时,它会按预期工作。

我真正想要的是告诉 Spring “加载此目录中的所有内容”

有任何想法吗?

您可以使用以下命令加载外部 jars

java -cp your.jar -Dloader.path=/home/user/lib org.springframework.boot.loader.PropertiesLauncher

您不想添加的 jars 应该被编译为不可执行的,没有 BOOT-INF

并添加依赖

        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-loader</artifactId>
    </dependency>

暂无
暂无

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

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