DEV Community

realNameHidden
realNameHidden

Posted on

πŸƒβ€β™‚οΈ Runners in Spring Boot – What They Are and How to Use Them

πŸƒβ€β™‚οΈ Runners in Spring Boot – What They Are and How to Use Them

Ever wondered how to run some code automatically when your Spring Boot application starts? That’s where runners come in!

Spring Boot gives you two simple ways to run logic right after the app starts:

  • CommandLineRunner
  • ApplicationRunner

Let’s break them down in a very easy way πŸ‘‡


βœ… What is a Runner?

A Runner is just a special class in Spring Boot that runs some code after the application starts, before the actual business logic begins.

Think of it as:

β€œDo this setup/check before the app starts fully!”


1️⃣ CommandLineRunner – Simple and Easy

  • You get the arguments passed to the app (String... args)
  • Best when you don’t need fancy argument handling

✨ Example:

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyStartupRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        System.out.println("βœ… App has started! CommandLineRunner running...");
    }
}
Enter fullscreen mode Exit fullscreen mode

As soon as your app runs, it will print that message in the console.


2️⃣ ApplicationRunner – Smarter with Arguments

  • It gives you ApplicationArguments so you can easily check --options passed
  • Great when you want to parse command-line inputs in a clean way

✨ Example:

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class MyAppRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("βœ… ApplicationRunner running...");
        System.out.println("πŸ‘‰ Option names: " + args.getOptionNames());
    }
}
Enter fullscreen mode Exit fullscreen mode

🎯 When to Use Runners?

Here are some real-world use cases:

Use Case Example
Load data into DB Seed test data in dev environment
Startup checks Validate file paths or connections
Cache setup Call external API and store config
Log start info Print custom banner or info

πŸ”’ What if You Have Multiple Runners?

No problem! You can control the order they run using @Order:

@Component
@Order(1)
public class FirstRunner implements CommandLineRunner {
    public void run(String... args) {
        System.out.println("🏁 First runner executed");
    }
}
Enter fullscreen mode Exit fullscreen mode

🧠 TL;DR (Quick Summary)

Feature CommandLineRunner ApplicationRunner
Arguments Type String[] ApplicationArguments
Simple Usage βœ… βœ…
Structured Arg Parsing ❌ βœ…
Real Use Cases Setup, logs, loading data Same, with better arg handling

βœ… Final Thoughts

Runners are a neat way to run code at startup without touching controllers or services. They're simple, powerful, and often used in real-world Spring Boot apps.


Top comments (0)