Application Startup

Basic Spring
≈ 2 min read

What happens after main()

In a Boot application, the main() method only starts the process. SpringApplication.run() prepares the environment, creates the context, registers configuration, creates beans, and completes startup only after the container is ready for normal work.

If the application needs to run a small piece of code immediately after the context starts successfully, you can use ApplicationRunner or CommandLineRunner.

@Component
class StartupRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) {
        System.out.println("Application is ready");
    }
}

The run() method is called after the context has been created, so a runner can receive other beans through regular Dependency Injection. This mechanism is suitable, for example, for small initialization or a check that must run at startup.

Startup is part of the lifecycle

The started context continues to live while the application is running. During a normal shutdown it is closed, and Spring runs bean destruction callbacks that we already encountered in the Spring Core lifecycle. So Boot does not replace that lifecycle — it organizes application startup and shutdown around it.

Do not turn a runner into an endless work loop. Its job is to perform limited startup work and return control. Long-running processes are better implemented with dedicated application mechanisms.