Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

What is the difference between ApplicationRunner and CommandLineRunner, when are they called, and what happens if one throws an exception?

Both run code once at startup; CommandLineRunner receives the raw String[] arguments, while ApplicationRunner receives them parsed as ApplicationArguments with options and non-option values. Boot calls both after the context is refreshed and before ApplicationReadyEvent, in the order set by @Order or Ordered. If one throws an exception, startup fails and the application exits, and because they run on the main thread, any long work in a runner delays readiness.

Why this question is asked

Most applications need to do something once at startup: seed some data, check a connection, print the active configuration or run a one-off command. Runners are Spring Boot's hook for that, and they are simple enough that their details get overlooked. Interviewers ask about them because those details, when they run, in what order and what an exception does, decide whether a service starts reliably. In this answer, we will compare the two interfaces with code, see where they run in the startup sequence, order them, and look at what happens when one fails or takes too long.

1The Two Interfaces Side by Side

Both interfaces have a single run method, and any bean that implements one of them is called by Boot at startup. The only difference is how the command-line arguments arrive.

Java
@Component
class RawArgsRunner implements CommandLineRunner {
@Override public void run(String... args) {
System.out.println(Arrays.toString(args)); // [--mode=report, --dry-run, orders.csv]
}
}
@Component
class ParsedArgsRunner implements ApplicationRunner {
@Override public void run(ApplicationArguments args) {
System.out.println(args.getOptionNames()); // [mode, dry-run]
System.out.println(args.getOptionValues("mode")); // [report]
System.out.println(args.containsOption("dry-run")); // true
System.out.println(args.getNonOptionArgs()); // [orders.csv]
}
}
// java -jar app.jar --mode=report --dry-run orders.csv

CommandLineRunner gives us exactly what main received, so we parse it ourselves. ApplicationRunner gives us ApplicationArguments, which already splits options written as --name=value from plain arguments such as a file name. Notice that options can repeat, so getOptionValues returns a list. If the runner needs arguments at all, ApplicationRunner saves parsing code; if it ignores them, either interface is fine. Both can also be defined as lambdas in @Bean methods.

2When Runners Are Called

Runners run late in SpringApplication.run. The context has been refreshed, which means every singleton bean exists and, in a web application, the embedded server is already listening. Boot then publishes ApplicationStartedEvent, calls the runners one after another on the main thread, and only after the last one returns does it publish ApplicationReadyEvent and set readiness to ACCEPTING_TRAFFIC.

Step throughWhere runners sit in startup
SpringApplicationContextRunnersProbes
refresh(): create beans, start server
refreshed, port open
ApplicationStartedEvent, liveness CORRECT
run() each runner in order, main thread
return
ApplicationReadyEvent
readiness ACCEPTING_TRAFFIC

7 messages in order. Play them, or step through them one at a time.

Everything a runner does happens before the application is reported ready.

The reason for this position is that runners can rely on every bean being ready. A runner can inject repositories, clients and properties freely, which is the point of using a runner instead of code in main.

3Ordering Several Runners

When there are several runners, Boot sorts all ApplicationRunner and CommandLineRunner beans together using @Order or the Ordered interface, lower values first. Runners without an order come last, in an order we should not rely on.

Java
@Component @Order(1)
class CheckSchemaRunner implements ApplicationRunner {
public void run(ApplicationArguments a) { System.out.println("check schema"); } // printed first
}
@Component @Order(2)
class SeedDataRunner implements CommandLineRunner {
public void run(String... a) { System.out.println("seed data"); } // printed second
}

Notice that the two interfaces are mixed in one ordered list, so an ApplicationRunner and a CommandLineRunner can be interleaved freely. If one runner depends on another's work, giving both an explicit order is the only safe way to express it.

4What Happens When a Runner Throws

A runner is part of startup, so an exception in it is a startup failure. Boot wraps it in an IllegalStateException that names the failed runner, publishes ApplicationFailedEvent, runs the failure analyzers, closes the context and rethrows the exception from SpringApplication.run. The remaining runners are not called.

Java
@Component
class SeedDataRunner implements CommandLineRunner {
public void run(String... a) {
throw new IllegalArgumentException("seed file missing");
}
}
// java.lang.IllegalStateException: Failed to execute CommandLineRunner
// Caused by: java.lang.IllegalArgumentException: seed file missing
// context closed, Tomcat stopped, process exits with a non-zero code

This is usually the right behaviour. If the runner does essential setup, an application without it should not serve traffic. But when the work is optional, such as warming a cache, the runner should catch and log its own exceptions, so a temporary problem does not stop the service from starting.

5Why Long Work in a Runner Delays Readiness

Because runners run on the main thread before ApplicationReadyEvent, a runner that takes two minutes keeps the application not ready for two minutes. In Kubernetes, the readiness probe keeps returning not ready, a rollout waits, and if the probes are configured strictly, the pod may even be restarted before it finishes.

So runners suit short tasks of a few seconds. Longer work should run in the background with readiness controlled explicitly, or move to a separate job. A good test is to ask whether the service could serve requests without this work being finished. If yes, it does not belong on the main thread at startup.

6Follow-up questions

Are runners called in tests with @SpringBootTest?

Yes. @SpringBootTest starts the application through SpringApplication, so runners run in every test context. A runner that seeds data or calls a remote system can surprise tests; guard it with a profile or a property, for example @ConditionalOnProperty.

Should I use @PostConstruct instead of a runner?

They run at different times. @PostConstruct runs while one bean is being created, when other beans may not exist yet and the server is not running. A runner runs after everything is ready, so it is the better place for startup work that uses several beans.

Does a runner run again if the context is refreshed?

No. Runners are called once by SpringApplication.run after the first refresh. They are an application startup hook, not a context event.

7Conclusion

In this answer, we saw that CommandLineRunner receives raw String arguments while ApplicationRunner receives parsed ApplicationArguments, and that Boot calls both after the context is refreshed and before ApplicationReadyEvent, on the main thread, in @Order order. An exception in a runner fails startup and the application exits, and long work in a runner delays readiness, so long tasks belong in the background or in a separate job.

What to say in the interviewAbout 80 seconds aloud

Open with

Both are Spring Boot hooks for running code once at startup, and the difference is only in the arguments. CommandLineRunner's run method gets the raw String array from main, so I parse it myself.

Then, if they want more

ApplicationRunner gets ApplicationArguments, which already splits options like --mode=report, with getOptionNames and getOptionValues, from non-option arguments like a file name. Boot calls both after the context has been refreshed, so all beans exist and the web server is already listening, and before ApplicationReadyEvent, which is when readiness becomes ACCEPTING_TRAFFIC.

They run one after another on the main thread, sorted together by @Order or Ordered, so if one runner depends on another, I give both an explicit order. If a runner throws, Boot treats it as a startup failure: it wraps it in an IllegalStateException, publishes ApplicationFailedEvent, closes the context and the application exits, and later runners do not run.

That is right for essential setup, but for optional work I would catch exceptions inside the runner. And because they block the main thread, long work in a runner delays readiness, so anything beyond a few seconds should go to the background or a separate job.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Boot reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the difference between ApplicationRunner and CommandLineRunner, when are they...": https://scrivio-interview-library.netlify.app/spring-boot/boot-startup/1b5a578ce8/