Running a Spring Boot App and CommandLineRunner
Running a Spring Boot Application
Section titled “Running a Spring Boot Application”Using the Main Method
Section titled “Using the Main Method”@SpringBootApplicationpublic class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}Using Maven
Section titled “Using Maven”mvn spring-boot:runRunning the Packaged JAR
Section titled “Running the Packaged JAR”java -jar app.jarCommandLineRunner
Section titled “CommandLineRunner”CommandLineRunner executes code immediately after the Spring Boot application has started.
@Componentpublic class StartupRunner implements CommandLineRunner {
@Override public void run(String... args) { System.out.println("Application started!"); }}Common Use Cases
Section titled “Common Use Cases”- Data initialization
- Cache preloading
- Startup validation
- Scheduled initialization tasks
CommandLineRunner vs ApplicationRunner
Section titled “CommandLineRunner vs ApplicationRunner”| Interface | Parameter |
|---|---|
CommandLineRunner |
String... args |
ApplicationRunner |
ApplicationArguments |
Key Takeaways
Section titled “Key Takeaways”mvn spring-boot:runis convenient for local development;java -jar app.jaris how it actually runs in production once packaged.CommandLineRunnerandApplicationRunnerboth run once, right after the application context is fully started — see the startup lifecycle for exactly where this fits.- Use
ApplicationRunneroverCommandLineRunnerwhen you need structured access to arguments (ApplicationArguments) rather than a rawString[].