-
Couldn't load subscription status.
- Fork 41.6k
Description
The Spring documentation states the following:
"An application is considered ready as soon as application and command-line runners have been called, see Spring Boot application lifecycle and related Application Events." (https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.spring-application.application-availability.readiness)
It also states the following:
"Tasks expected to run during startup should be executed by CommandLineRunner and ApplicationRunner components instead of using Spring component lifecycle callbacks such as @PostConstruct. "
I think what is missing here is the fact that you cannot use CommandLineRunners or ApplicationRunners if you want to have some code executed and finished BEFORE the ApplicationReadyEvent is fired and BEFORE the application accepts network requests.
It look me a while to find out that I can achieve what I want using a SmartInitializingSingleton. But it was a quite long journey to find that one out.
I think the existence of SmartInitializingSingleton and its purpose should be mentioned next to the description of the Readiness state (https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.spring-application.application-availability.readiness), so that other people don't fall into that trap.
PS: This is the discussion that caused this issue: https://stackoverflow.com/questions/77968217/spring-boot-accepts-network-requests-before-applicationrunners-are-done/77968634#77968634