Spring Boot - Best way to start a background thread on deployment

You could have a bean which impelements ApplicationListener<ContextRefreshedEvent> It's onApplicationEvent will be called just start your thread there if it hasn't been started already. I think you want the ApplicationReadyEvent by the way.

Edit How to add a hook to the application context initialization event?

@Component
public class FooBar implements ApplicationListener<ContextRefreshedEvent> {

    Thread t = new Thread();

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if (!t.isAlive()) {
            t.start();
        }
    }
}

The main method is not called when deploying the application to a non-embedded application server. The simplest way to start a thread is to do it from the beans constructor. Also a good idea to clean up the thread when the context is closed, for example:

@Component
class EventSubscriber implements DisposableBean, Runnable {

    private Thread thread;
    private volatile boolean someCondition;

    EventSubscriber(){
        this.thread = new Thread(this);
        this.thread.start();
    }

    @Override
    public void run(){
        while(someCondition){
            doStuff();
        }
    }

    @Override
    public void destroy(){
        someCondition = false;
    }

}