Service Startup Failure Caused by @EventListener
Using @EventListener({ApplicationReadyEvent.class}) allows you to execute some special business data operations after the service starts.
However, it's important to note that this method is called by the main thread. If an exception occurs in this method, it will cause the entire service to fail to start.

Solution
The solution is naturally to put the event handling in an asynchronous thread.
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
@Component
@EnableAsync
public class MyApplicationListener {
@Async
@EventListener({ApplicationReadyEvent.class})
public void onApplicationReady() {
// This code will execute in a separate thread, not the main thread
System.out.println("Application is ready! Executing in a separate thread.");
}
}
The onApplicationReady method will execute in a separate thread instead of the main thread. This prevents long-running operations from blocking the main thread.
