【问题标题】:Can I create multiple entry points to a Spring Boot app?我可以为 Spring Boot 应用程序创建多个入口点吗?
【发布时间】:2017-08-30 10:37:56
【问题描述】:

Spring Boot中,需要指定一个主类,它是应用程序的入口点。通常这是一个带有标准 main 方法的简单类,如下所示;

@SpringBootApplication
public class MySpringApplication {

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

然后,当应用程序运行时,该类被指定为主入口点。

但是,我想使用不同的主类运行我的代码,使用 config 来定义它,并且不使用不同的jar!! (我知道重建 jar 将使我能够指定一个替代主类,但这实际上给了我两个应用程序,而不是一个!那么,我怎样才能做到这一点来利用一个 jar 和两个主类并选择一个使用通过 Spring application.yml 文件?

【问题讨论】:

  • 目的是什么?
  • Spring Boot 的入口点看起来像SpringApplication.run(className, args),你为什么要创建自己的类的实例,里面的代码是什么?
  • @AndresToilko 我想将 CLI 包含到 SpringBoot 作为设置测试的单独入口点 - 面临同样的问题。

标签: java spring-boot config


【解决方案1】:

我找到了答案 - 使用 CommandLineRunner 界面...

所以现在我有两个班级;

public class ApplicationStartupRunner1 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
    //implement behaviour 1 
}

public class ApplicationStartupRunner2 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
    //implement behaviour 2
}

以及如何在配置中切换它们..

@Configuration
public class AppConfig {

    @Value("${app.runner}")
    private int runner;

    @Bean
    CommandLineRunner getCommandLineRunner() {
        CommandLineRunner clRunner = null;
        if (runner == 1) {
            clRunner = new ApplicationStartupRunner1();
        } (else if runner == 2) {
            clRunner = new ApplicationStartupRunner2();
        } else {
            //handle this case..
        }

        return clRunner;
    }
}

最后在 application.properties 文件中,使用

app.runner=1

【讨论】:

    【解决方案2】:

    我会坚持只有一个主类和一个主方法的原始模式,但是在那个方法中你可以配置你想去的地方。 IE。而不是拥有 2 个主要方法并配置哪个被调用,使这 2 个方法只是普通方法,并创建一个使用配置来确定两个方法中的哪一个运行的主要方法。

    【讨论】:

    • 不幸的是,在调用 application.run 方法之后才加载 Spring 配置,因此 main 方法在配置已知之前已经运行。
    【解决方案3】:

    jonny.l 的回答很好。 另一个非常相似但更手动/DIY 的解决方案是获取ApplicationContext,您可以从中获取所有其他内容:

    @SpringBootApplication
    public class App {
        public static void main(String[] args) {
            ConfigurableApplicationContext context = SpringApplication.run(App.class);
            ConfigurableEnvironment env = context.getBean(ConfigurableEnvironment.class);
            
            String mainApp = env.getProperty("app.runner");
            if (mainApp.equals("Main1")) {
                Main1 main1 = context.getBean(Main1.class);
                main1.run();
            } else if (mainApp.equals("Main2")) {
                Main2 main2 = context.getBean(Main2.class);
                main2.run();
            }
        }
    }
    
    @Service @Lazy
    public class Main1 {
        public void run() {}
    }
    
    @Service @Lazy
    public class Main2 {
        public void run() {}
    }
    

    @Lazy用于防止那些 bean 不必要地自动加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-23
      • 2014-11-17
      • 2018-04-04
      • 2017-12-13
      • 2021-06-09
      • 1970-01-01
      • 2020-05-17
      • 1970-01-01
      相关资源
      最近更新 更多