Spring Boot ApplicationRunner
June 19, 2024
Spring Boot ApplicationRunner
is a runner that runs before the application gets ready to serve traffic. It is useful to run any logic before application starts up.
Spring Boot has two runners,
CommandLineRunner
and ApplicationRunner
with the difference that CommandLineRunner
accepts arguments as string array whereas ApplicationRunner
accepts arguments as ApplicationArguments
. Both are functional interface with a method named as run
.
Find the method declaration of
ApplicationRunner
from Spring doc.
void run(ApplicationArguments args)
ApplicationArguments: Interface whose role is to provide access to the arguments used to run
SpringApplication
in Spring Boot application. ApplicationArguments
has following methods.
1.
boolean containsOption(String name)
List<String> getNonOptionArgs()
Set<String> getOptionNames()
List<String> getOptionValues(String name)
String[] getSourceArgs()
Using ApplicationRunner
Create a component by implementingApplicationRunner
.
@Component public class MyAppRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { Arrays.stream(args.getSourceArgs()).forEach(arg -> System.out.println(arg)); } }
SpringApplication
.
Find Spring Boot Main class.
@SpringBootApplication public class MyApp { public static void main(String[] args) { SpringApplication application = new SpringApplication(MyApp.class); application.run(args); } }
java -jar spring-boot-app.jar Hello
Hello
Implement ApplicationRunner by Main Class
We can create runner directly by implementing Main class. Overdriverun
method and perform any logic.
@SpringBootApplication public class TadStackApplication implements ApplicationRunner { public static void main(String[] args) { SpringApplication application = new SpringApplication(TadStackApplication.class); application.run(args); System.out.println("----Done----"); } @Override public void run(ApplicationArguments args) throws Exception { System.out.println("Running scripts..."); } }
Running scripts... ----Done----
Register Runner using @Bean
We can register runner with Spring using@Bean
annotation.
@SpringBootApplication public class TadStackApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(TadStackApplication.class); application.run(args); System.out.println("----Done----"); } } @Configuration class TSAppConfig { @Bean public MyTSRunner getMyTSRunner() { return new MyTSRunner(); } } class MyTSRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("Running scripts..."); } }
Creating ApplicationRunner using Lambda Expression
ApplicationRunner
is a functional interface. We can create their object using lambda expression.
@SpringBootApplication public class TadStackApplication { public static void main(String[] args) { SpringApplication application = new SpringApplication(TadStackApplication.class); application.run(args); System.out.println("----Done----"); } } @Configuration class TSAppConfig { @Bean public ApplicationRunner getMyTSRunner() { return args -> { System.out.println("args: " + args.getOptionNames()); }; } }
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8282 --spring.profiles.active=dev"
args: [server.port, spring.profiles.active] ----Done----
Set Order
When we have more than one runner, we can set the ordering of runners usingOrdered
interface or @Order
annotation.
Find the first runner with order one.
@Order(1) @Component public class XyzRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("XyzRunner..."); } }
@Order(2) @Component public class AbcRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println("AbcRunner..."); } }
XyzRunner... AbcRunner...
Usability of ApplicationRunner
1. Database transaction :ApplicationRunner
is useful to perform database transaction before application starts up. Suppose our application has to insert some seed data in the database before handling traffic, we can use the runner.
2. Running Scheduler : Application can run schedulers before application starts handling traffic. Runner is useful to run schedulers using arguments from command line.
3. Logging Messages : We can log messages before application starts up.