Spring Boot ApplicationRunner

By Arvind Rai, 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) 
Callback method used to run bean.

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) 
2.
List<String> getNonOptionArgs() 
3.
Set<String> getOptionNames() 
4.
List<String> getOptionValues(String name) 
5.
String[] getSourceArgs() 

Using ApplicationRunner

Create a component by implementing ApplicationRunner.
@Component
public class MyAppRunner implements ApplicationRunner {
	@Override
	public void run(ApplicationArguments args) throws Exception {
		Arrays.stream(args.getSourceArgs()).forEach(arg -> System.out.println(arg));
	}
} 
This runner will automatically be registered with 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);
	}
} 
Let us pass some arguments while running application from command prompt.
java -jar spring-boot-app.jar Hello 
Output
Hello 

Implement ApplicationRunner by Main Class

We can create runner directly by implementing Main class. Overdrive run 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...");
	}
} 
Output
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());
		};
	}
} 
Run the command from command line.
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8282 --spring.profiles.active=dev" 
Find the output.
args: [server.port, spring.profiles.active]
----Done---- 

Set Order

When we have more than one runner, we can set the ordering of runners using Ordered 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...");
	}
} 
Find the second runner with order two.
@Order(2)
@Component
public class AbcRunner implements ApplicationRunner {
 	@Override
	public void run(ApplicationArguments args) throws Exception {
		System.out.println("AbcRunner...");
	}
} 
Output
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.

Reference





©2024 tadstack.com | Privacy Policy | Contact Us