Documentation Index
Fetch the complete documentation index at: https://mintlify.com/spring-projects/spring-boot/llms.txt
Use this file to discover all available pages before exploring further.
SpringApplication is the entry point that bootstraps a Spring application from a Java main method. It creates the appropriate ApplicationContext, registers a CommandLinePropertySource to expose command-line arguments as Spring properties, refreshes the context to load all singleton beans, and then triggers any CommandLineRunner or ApplicationRunner beans.
Running your application
In most cases you delegate to the staticSpringApplication.run(...) method:
SpringApplication instance and configure it before calling run:
INFO-level log messages are shown at startup. Set spring.main.log-startup-info=false to suppress the startup banner and active-profile log lines.
Application startup events
Spring Boot publishes a sequence of events as your application starts. Some events fire before theApplicationContext is created, so you cannot register listeners for them as @Bean definitions — use SpringApplication.addListeners(...) or SpringApplicationBuilder.listeners(...) instead.
ApplicationStartingEvent
Sent at the very start of a run, before any processing other than listener and initializer registration.
ApplicationEnvironmentPreparedEvent
Sent when the
Environment is known but before the context is created.ApplicationContextInitializedEvent
Sent after the
ApplicationContext is prepared and ApplicationContextInitializers have been called, but before bean definitions are loaded.ApplicationStartedEvent
Sent after the context is refreshed, but before application and command-line runners are called.
ApplicationReadyEvent
Sent after all
ApplicationRunner and CommandLineRunner beans have been called. At this point the application is ready to service requests.META-INF/spring.factories entry:
Event listeners run in the same thread as the application by default. Avoid running lengthy tasks inside them — use
ApplicationRunner or CommandLineRunner instead.ApplicationRunner and CommandLineRunner
Both interfaces let you execute code once the application has started but before it begins accepting traffic. Implement either interface and register the class as a Spring bean.CommandLineRunner receives the raw command-line arguments as a String array:
ApplicationRunner receives the same arguments as a structured ApplicationArguments object, giving you convenient access to option arguments (those prefixed with --) separately from non-option arguments:
Ordered or add an @Order annotation.
Fluent builder API and parent/child contexts
SpringApplicationBuilder provides a fluent API for building ApplicationContext hierarchies. This is useful when you need a parent context that owns shared beans and one or more child contexts that own web-tier beans:
Web components must be contained in the child context. Both parent and child contexts share the same
Environment.Lazy initialization
When lazy initialization is enabled, beans are created on first use rather than at startup, which can significantly reduce startup time.@Lazy(false).
Startup failure analysis
When your application fails to start, registeredFailureAnalyzer beans provide a human-readable error message and suggested action. For example, if port 8080 is already in use:
--debug to display the full conditions evaluation report:
Graceful shutdown
EachSpringApplication registers a shutdown hook with the JVM so the ApplicationContext closes gracefully on exit. All standard Spring lifecycle callbacks — DisposableBean, @PreDestroy — are honored during shutdown.
Beans that need to communicate a specific exit code implement ExitCodeGenerator: