Spring boot使用延迟启动webapplication programaticaly

时间:2015-11-17 10:30:08

标签: java spring spring-mvc spring-boot

我喜欢春季靴子,但我无法想象/我怎么能实现这个目标: 我有可执行的spring应用程序。我需要applicationContext,做一些事情然后开始" webPart" (REST api)。是否有可能告诉spring"不要自动启动jetty,我自己启动#34;或者我需要自己编写应用程序?

我想做那样的事情。有人有任何想法吗?

@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"但是后来当我决定(f.e。没有抛出异常......)。我根本不想阻止使用Web上下文。

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

你需要像在这里How to prevent auto start of tomcat/jetty in Spring Boot when I only want to use RestTemplate

那样自动阻止jetty / tomcat启动