1

I like spring boot but I can't figure/find out how can I achieve this: I have executable spring application. I need applicationContext, do some stuff and then start "webPart" (REST api). Is it possible to tell spring "don't start jetty automatically, I'll start it myself", or I need to compose application myself?

I would like to do something like that. Do anyone have any idea?

@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();
        }
    }
}

EDIT: I want to use "web-part" but later and when I decided to (f.e. Exception is not thrown...). I don't want to prevent web-context to be used at all.

Cœur
  • 37,241
  • 25
  • 195
  • 267
svobol13
  • 1,842
  • 3
  • 25
  • 40
  • Possible duplicate of [How to prevent auto start of tomcat/jetty in Spring Boot when I only want to use RestTemplate](http://stackoverflow.com/questions/31897165/how-to-prevent-auto-start-of-tomcat-jetty-in-spring-boot-when-i-only-want-to-use) – Hexaholic Nov 17 '15 at 10:54
  • I think my problem is different. I dont want to not use web-context I want to let it wait till I say its time to start web-part. – svobol13 Nov 17 '15 at 11:00

2 Answers2

2

One way is to destroy the first context (which has web disabled) and then start a new context with web enabled.

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);
Mohit
  • 1,740
  • 1
  • 15
  • 19
0

You need to prevent jetty/tomcat start automatically like they did here How to prevent auto start of tomcat/jetty in Spring Boot when I only want to use RestTemplate

Community
  • 1
  • 1
codeaholicguy
  • 1,671
  • 1
  • 11
  • 18