Spring Boot Runners

Abhishek Singh
3 min readMar 11, 2021

--

A Runner is an auto-executable component which is
called by container on application startup only once,
In simple this concept is used to execute any logic
(code) one time when application is started,

Spring Boot provides two interfaces, CommandLineRunner
and ApplicationRunner, to run specific pieces of code
when an application is fully started, These interfaces
gets called just before run() method once Spring Application completes,

In spring boot application we can execute any task just before
spring boot finishes its startup. To do so we need to create
spring bean using CommandLineRunner or ApplicationRunner interface
and spring boot will automatically detect them. Both the interfaces
have run() method that needs to be overridden in implementing class
and make the class as bean by using spring stereotype such as @Component
CommandLineRunner and ApplicationRunner serve the same purpose. The difference between CommandLineRunner and ApplicationRunner is that
the run() method of CommandLineRunner accepts array of String as an
argument and run() method of ApplicationRunner accepts spring
ApplicationArguments as an argument. The arguments which we pass
to main() method while starting spring boot, can be accessed in
the run() method of CommandLineRunner and ApplicationRunner
implementation classes. We can create more than one bean of
CommandLineRunner and ApplicationRunner implementing classes,
To execute them in an order, we use spring @Order annotation
or Ordered interface, The run() method of CommandLineRunner
and ApplicationRunner are executed just before SpringApplication
finishes its startup, After startup completes, application starts
to run. The usability of CommandLineRunner and ApplicationRunner
are that we can start any scheduler or log any message before
application starts to run.

Types of Runners:
There are two types of Runners:
CommandLineRunner
ApplicationRunner

These runners are used to run the logic on application startup,
for example spring boot has ApplicationRunner(Functional Interface)
with run() method, ApplicationRunner run() will get executed just
after ApplicationContext is created and before spring boot application
startup, ApplicationRunner takes ApplicationArgument which has convenient
methods like getOptionNames(), getOptionValues() and getSourceArgs()

And CommandLineRunner is also a Functional interface with run() method,
CommandLineRunner run() will get executed just after ApplicationContext
is created and before spring boot application starts up, It accepts the
argument which are passed at time of server startup,multiple CommandLineRunner beans can be defined within the same application context and can be ordered using the @Ordered interface or @Order annotation

Both of them provides the same functionality and the only difference
between CommandLineRunner and ApplicationRunner is CommandLineRunner.run() accepts String array[] whereas ApplicationRunner.run() accepts ApplicationArguments as argument,

CommandLineRunner :

This is a legacy runner(old one) which is provided in
spring boot 1.0 version, It has only one abstract method
run(String… args) : void It is a Functional Interface (having only one abstract method) Add one stereotype Annotation over Implementation class level
e.g @Component so that container can detect the class and create
object to it,

This interface provides access to application arguments as
string array, Let’s see the example code for more clarity.

@Component
public class CommandLineAppStartupRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandLineAppStartupRunner.class);

@Override
public void run(String…args) throws Exception {
logger.info(“Application started with command-line arguments: {} . \n To kill this application, press Ctrl + C.”, Arrays.toString(args));
}
}

ApplicationRunner:

ApplicationRunner wraps the raw application arguments and exposes
the ApplicationArguments interface, which has many convinent methods
to get arguments, like getOptionNames() to return all the arguments
names, getOptionValues() to return the agrument value, and raw source
arguments with method getSourceArgs()

Let’s see an example:

@Component
public class AppStartupRunner implements ApplicationRunner {

private static final Logger logger = LoggerFactory.getLogger(AppStartupRunner.class);

@Override
public void run(ApplicationArguments args) throws Exception {

logger.info(“Your application started with option names : {}”, args.getOptionNames());
}
}

When to Use It:
When you want to execute some piece of code exactly before the
application startup completes, you can use it then, In one of
our projects, we used these to source data from other microservices
via service discovery, which was registered in Consul

Ordering :
You can register as many Application/CommandLine runners as you want,
You just need to register them as Beans in the application context,
Then, spring will automatically pick them up, You can order them as
well either by extending interface org.springframework.core.Ordered
or via the @Order annotation,

Thats all as of my knowledge..Keep Learning..

--

--

Abhishek Singh
Abhishek Singh

Written by Abhishek Singh

Engineer | Blogger | Traveller

No responses yet