AI Hints
Home›Spring Boot Tutorial›CommandLineRunner, Environment Variables and Practice: A Configurable Shop
Spring Boot · Lesson 43 of 120

CommandLineRunner

Code at startup.

⏱ 2 min read▶ Video at 57:16🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Sometimes you want code to run once, when the app starts. Loading some data, checking a setting, or printing a welcome. Command Line Runner does exactly that.

  • Run once
  • At startup
  • CommandLineRunner

How it works

Command Line Runner is an interface, with one method, run. Any bean that implements it, is run by Spring, after the app has started. Run receives the command line arguments. And if there are several runners, each one runs once.

keyvalue
InterfaceCommandLineRunner
Methodrun(String... args)
Whenright after startup
Several?each runs once

A startup runner

Startup runner is a component, that implements Command Line Runner. Spring calls its run method, once, at startup. Here, it logs two messages. Run it. Both messages appear right after the app has started, before any request arrives.

StartupRunner.javajava
@Component
public class StartupRunner implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(StartupRunner.class);

    @Override
    public void run(String... args) {
        log.info("The shop is starting up");
        List<String> stock = List.of("Dune", "Circe", "The Hobbit");
        log.info("Checked stock: {} books", stock.size());
    }
}
Output
INFO  StartupRunner : The shop is starting up
INFO  StartupRunner : Checked stock: 3 books

A runner as a bean

A runner can also be a bean method, that returns a lambda. It can ask for other beans, like shop properties. And it prints the arguments, passed when the app was started. Run it. We start the app with an extra argument, dash dash greeting equals hi. The runner prints the welcome, the discount, and the arguments.

ShopConfig.javajava
@Configuration
public class ShopConfig {

    @Bean
    CommandLineRunner welcome(ShopProperties shop) {
        return args -> {
            System.out.println("Welcome to " + shop.name());
            System.out.println("Today's discount: " + shop.discount() + "%");
            System.out.println("Arguments: " + Arrays.toString(args));
        };
    }
}
Output
Welcome to Corner Books
Today's discount: 10%
Arguments: [--greeting=hi]
Also uses ShopProperties.java
@ConfigurationProperties("shop")
public record ShopProperties(String name, int discount) {}
Also uses BookshopApplication.java
@SpringBootApplication
@ConfigurationPropertiesScan
public class BookshopApplication {
    public static void main(String[] args) {
        SpringApplication.run(BookshopApplication.class, args);
    }
}
Also uses application.properties
shop.name=Corner Books
shop.discount=10
Also uses _args
--greeting=hi

Summary

Command Line Runner runs code once, right after the app starts. Implement it on a component, or return one from an at Bean method. Next, environment variables.

Key takeaways
  • CommandLineRunner
  • run(String... args)
  • Once, at startup
Next up: Environment Variables — settings from outside