Skip to content
Advertisement

How starter-web prevent spring-boot exit?

When using spring-boot-starter-web, The spring-boot application won’t exit after startup.

And with Jetbrain IDEA, There is an icon show spring-boot start up finished:

enter image description here

enter image description here

But if I using :

public static void main ( String[] args ){
    SpringApplication.run(Application.class, args);
    Thread.sleep(Long.MAX_VALUE);
}

Or

public class MyRunner implements ApplicationRunner{
    public void run() {
        Thread.sleep(Long.MAX_VALUE);
    }
}

Can let spring-boot keep running but the IDEA icon will loading forever, So that must be different way compare with starter-web.

Update1: And those two method will cause SpringBootTest wait forever

Question: What’s the code that spring-boot-starter-web prevent spring-boot exit ?

Advertisement

Answer

To answer the question

What’s the code that spring-boot-starter-web prevent spring-boot exit

spring-boot-web creates web server beans, that use non-daemon listener threads (as you want them to be alive and running to waiting for incoming connections and handle them).

This makes your code reach end of your main method, however JVM does not exit, as it has non-daemon threads running.

If you want to keep your application alive, you’d need to do the same – the simplest way would be to e.g. create a Thread in a bean like @Slongtong has suggested.

Advertisement