在春季启动中加载外部罐子



我有一个包含此类的jar文件:

@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 的软件包属于 com.xxx - 所以应该扫描它以查找组件是否正确?

我做错了什么? 如何加载外部罐子并让 Spring 像往常一样加载它。 我不想把它添加到我的pom.xml。
编辑 当我将它添加到我的pom中时,它会按预期工作。

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

有什么想法吗?

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

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

你不想添加的 jar 应该编译为不可执行的,没有 BOOT-INF

并添加依赖项

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

最新更新