How to use ApplicationRunner in Spring Boot application?

The Application Runner interface provided by Spring Boot is a little advance than the CommandLineRunner Interface. In simple words, we can say that CommandLineRunner Interface does not do more than executing the run() method before the completion of the Spring Boot Application Life Cycle and putting all the program arguments in an array of String.

So, the question comes what is special about “ApplicationRunner”, well It allows you to define the following arguments –

  1. Optional Arguments
  2. Non-Optional Arguments

In this post, we will take a deep dive into the Spring Boot application runner along with an example. You can find the same code which I have used in this post onto my GitHub repo-Clone Repo

Note:- With the new release of Spring Boot v2.6 there are nothing changes in terms of application runner

Table of Content

  1. Why do we need ApplicationRunner
  2. How to define the “Optional” and “Non-Optional” arguments?
  3. Let’s Jump to Code
  4. Console Output
  5. When to use ApplicationRunner?
  6. ApplicationRunner vs CommandLineRunner
  7. Conclusion

1. Why do we need ApplicationRunner?

Let’s say you have need to run a Scheduled batch Job, set some system environment properties, or need to perform some DB operation just before the Spring Boot run() method is finished, so in this kind of scenario ApplicationRunner Interface comes handy. It allows you to do such operations before the Spring Boot’s run() method finishes its execution.

On top of that, we have control over the arguments to make them optional and non-optional.

Here are some of the use cases where you will need Spring Boot Application Runner -

  1. Batch Job - Trigger a batch job before Spring Boot application startup is complete
  2. Initializing Cache/Loading addition config - There are a couple of state check which Spring Boot provide (Readiness and Liveness). Liveness is something when your application is up and running while Readiness is a state where your Spring Boot started accepting traffic. So using Liveness and Readiness we can delegate the other tasks such as initializing cache, loading addition data, loading config files to Application Runner.
  3. Log Command Line Arguments - A Developer can also log some additional command line argument during the application startup using Spring Boot Application Runner.

2. How to define the “Optional” and “Non-Optional” arguments?

Example (Running the spring boot from terminal)-

1Java -jar spring-boot-application-runner.jar nonoptionarg --optionalArg1=Jhooq --optionalArg2=ApplicationRunner

3. Let’s Jump to Code

As per API documentation, ApplicationRunner is an Interface, so we cannot create an instance of it, but we can implement it.

 2 * @author Rahul Wagh
 3 */
 4public class SpringBootApplicationRunner implements ApplicationRunner {
 6 private static Logger LOG = LoggerFactory
 7         .getLogger(SpringBootApplicationRunner.class);
 9 public static void main(String] args) {
10"STARTING : Spring boot application starting");
11, args);
12"STOPPED  : Spring boot application stopped");
13 }
15 @Override
16 public void run(ApplicationArguments args) {
17"EXECUTING : Run method of Application Runner");
18  final List nonOptionArgs = args.getNonOptionArgs();
19  final String] sourceArgs = args.getSourceArgs();
20  final Set  optionNames = args.getOptionNames();
22  nonOptionArgs.forEach(nonOption ->"## Non Option Args : "+nonOption));
23  optionNames.forEach(option ->"## Option Names    : "+option));
24 ->"## Source Args     : "+srcArgs));
25"## Option Value of --optionalArg1 : "+args.getOptionValues("optionalArg1"));
26"## Option Value of --optionalArg2 : "+args.getOptionValues("optionalArg2"));
28 }

Example (Running the spring from IntelliJ)-

Application Runner Runtime Arguments

1Java -jar spring-boot-application-runner.jar nonoptionarg --optionalArg1=Jhooq --optionalArg2=ApplicationRunner

4.Console Output

 1 main]: Starting SpringBootApplicationRunner on Rahul-PC with PID 11032
 2 main]: No active profile set, falling back to default profiles: default
 3 main]: Started SpringBootApplicationRunner in 0.705 seconds (JVM running for 1.413)
 4 main]: EXECUTING : Run method of Application Runner
 5 main]: ## Non Option Args : nonoptionarg
 6 main]: ## Option Names    : optionalArg2
 7 main]: ## Option Names    : optionalArg1
 8 main]: ## Source Args     : nonoptionarg
 9 main]: ## Source Args     : --optionalArg1=Jhooq
10 main]: ## Source Args     : --optionalArg2=ApplicationRunner
11 main]: ## Option Value of --optionalArg1 : Jhooq]
12 main]: ## Option Value of --optionalArg2 : ApplicationRunner]
13 main]: STOPPED  : Spring boot application stopped

5. When to use ApplicationRunner?

ApplicationRunner can be used in the following scenarios: –

  1. Need to add some additional logger information
  2. Schedule a batch job
  3. Database operation i.e. cleanup script, status update

6. ApplicationRunner vs CommandLineRunner

  1. ApplicationRunner and CommandLineRunner both are functional interfaces provided by the spring boot
  2. Both ApplicationRunner and CommandLineRunner have run() method which needs to be implemented by the implementor class.
  3. Method signature for run() is different in ApplicationRunner and in CommandLineRunner. ApplicationRunner – run(ApplicationArguments args) ,CommandLineRunner – run(String… args)
  4. In CommandLineRunner the runtime arguments can be accessed as an Array of strings e.g. Arrays.toString(args).
  5. While in ApplicationRunner the runtime arguments can be accessed as optionNames e.g. – args.getNonOptionArgs() and nonOptionArgs e.g. – args.getNonOptionArgs()

7. Conclusion

I hope this little tutorial might have helped you to get a better understanding of “How to use ApplicationRunner Interface”. If you have any questions or queries please put your comments below and I will be happy to answer you.

References - When and why we need Spring Boot Application Runner

Want to learn more about deploying Spring Boot on Kubernetes - Click Here