Skip to content

Running a Spring Boot App and CommandLineRunner

@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Terminal window
mvn spring-boot:run
Terminal window
java -jar app.jar

CommandLineRunner executes code immediately after the Spring Boot application has started.

@Component
public class StartupRunner implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("Application started!");
}
}
  • Data initialization
  • Cache preloading
  • Startup validation
  • Scheduled initialization tasks
Interface Parameter
CommandLineRunner String... args
ApplicationRunner ApplicationArguments
  • mvn spring-boot:run is convenient for local development; java -jar app.jar is how it actually runs in production once packaged.
  • CommandLineRunner and ApplicationRunner both run once, right after the application context is fully started — see the startup lifecycle for exactly where this fits.
  • Use ApplicationRunner over CommandLineRunner when you need structured access to arguments (ApplicationArguments) rather than a raw String[].