The ContextStartedEvent is not getting fired in Spring Boot application when application is started with this format -

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Component
    class ContextStartedListener implements ApplicationListener<ContextStartedEvent> {

        @Override
        public void onApplicationEvent(ContextStartedEvent event) {
            // THIS IS NOT TRIGGRED
            System.out.println("Context started");
        }
    }

    @Component
    class ContextRefreshesListener implements ApplicationListener<ContextRefreshedEvent> {

        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            // THIS IS TRIGGRED
            System.out.println("Context refreshed");
        }
    }
}

Similar Overflow - ContextStartedEvent not firing in Spring Boot

Comment From: philwebb

The ContextStartedEvent is being published, but it's before any beans are registered so it's already happened by the time you ContextStartedListener @Component is created. You can use SpringApplication.addListeners if you want to listen for early events.