【问题标题】:Spring boot Start webapplication with delay programmaticallySpring boot 以编程方式启动具有延迟的 Web 应用程序
【发布时间】:2015-11-17 10:30:07
【问题描述】:

我喜欢弹簧靴,但我不知道如何实现这一点: 我有可执行的弹簧应用程序。我需要applicationContext,做一些事情,然后启动“webPart”(REST api)。是否可以告诉spring“不要自动启动jetty,我自己启动”,或者我需要自己编写应用程序?

我想做这样的事情。有人知道吗?

@SpringApplication
public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(App.class, args);
        try {
            doSth(ctx);
            startWeb();
        } catch(Exception e) {
            clean();
        }
    }
}

编辑: 我想使用“web-part”,但后来当我决定使用时(例如,没有抛出异常......)。我根本不想阻止使用网络上下文。

【问题讨论】:

标签: java spring spring-mvc spring-boot


【解决方案1】:

一种方法是销毁第一个上下文(禁用 Web),然后启动一个启用 Web 的新上下文。

ConfigurableApplicationContext ctx = new SpringApplicationBuilder(Application.class).web(false).run(args);

try {
    doSomething(ctx);
} catch (Exception e){
    //abort
} finally {
    ctx.close();
}

//New context has web enabled.
ctx = new SpringApplicationBuilder(Application.class).run(args);
doSomething(ctx);

【讨论】:

    【解决方案2】:

    你需要像他们在How to prevent auto start of tomcat/jetty in Spring Boot when I only want to use RestTemplate987654321@ 那样防止码头/tomcat 自动启动

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-26
      • 2020-12-06
      • 2017-01-16
      • 1970-01-01
      • 2017-02-24
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多