Skip to main content

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 static SpringApplication.run(...) method:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}
For more control, create a SpringApplication instance and configure it before calling run:
public static void main(String[] args) {
    SpringApplication application = new SpringApplication(MyApplication.class);
    // ... customize application settings here
    application.run(args);
}
By default, 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 the ApplicationContext is created, so you cannot register listeners for them as @Bean definitions — use SpringApplication.addListeners(...) or SpringApplicationBuilder.listeners(...) instead.
1

ApplicationStartingEvent

Sent at the very start of a run, before any processing other than listener and initializer registration.
2

ApplicationEnvironmentPreparedEvent

Sent when the Environment is known but before the context is created.
3

ApplicationContextInitializedEvent

Sent after the ApplicationContext is prepared and ApplicationContextInitializers have been called, but before bean definitions are loaded.
4

ApplicationPreparedEvent

Sent just before refresh starts, after bean definitions have been loaded.
5

ApplicationStartedEvent

Sent after the context is refreshed, but before application and command-line runners are called.
6

ApplicationReadyEvent

Sent after all ApplicationRunner and CommandLineRunner beans have been called. At this point the application is ready to service requests.
7

ApplicationFailedEvent

Sent if there is an exception during startup.
To automatically register a listener regardless of how the application is created, add a META-INF/spring.factories entry:
org.springframework.context.ApplicationListener=com.example.project.MyListener
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:
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // run after the application context has started
    }

}
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:
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // args.getOptionNames(), args.getOptionValues("name"), etc.
    }

}
When you need a specific ordering, implement 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:
import org.springframework.boot.builder.SpringApplicationBuilder;

new SpringApplicationBuilder()
    .sources(ParentConfig.class)
    .child(ChildConfig.class)
    .run(args);
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.
spring:
  main:
    lazy-initialization: true
You can also enable it programmatically:
SpringApplication application = new SpringApplication(MyApplication.class);
application.setLazyInitialization(true);
application.run(args);
Lazy initialization defers the discovery of configuration errors — a misconfigured bean will only fail when it is first used, not at startup. Ensure you have tuned your JVM heap size appropriately, because all beans still need to fit in memory even if they are created later.
To exclude specific beans from lazy initialization while the rest of the application initializes lazily, annotate them with @Lazy(false).

Startup failure analysis

When your application fails to start, registered FailureAnalyzer beans provide a human-readable error message and suggested action. For example, if port 8080 is already in use:
***************************
APPLICATION FAILED TO START
***************************

Description:

Embedded servlet container failed to start. Port 8080 was already in use.

Action:

Identify and stop the process that is listening on port 8080 or configure this application to listen on another port.
If no failure analyzer can handle the exception, run with --debug to display the full conditions evaluation report:
$ java -jar myproject-0.0.1-SNAPSHOT.jar --debug

Graceful shutdown

Each SpringApplication 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:
import org.springframework.boot.ExitCodeGenerator;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class MyApplication {

    @Bean
    public ExitCodeGenerator exitCodeGenerator() {
        return () -> 42;
    }

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

}

Virtual threads

Virtual threads require Java 21 or later; Java 24 or later is strongly recommended for the best experience. Enable them with:
spring:
  threads:
    virtual:
      enabled: true
Virtual threads are daemon threads. If all JVM threads are daemon threads, the JVM exits. When using virtual threads with scheduled tasks or other long-running work, set spring.main.keep-alive=true to prevent premature JVM exit.

Build docs developers (and LLMs) love