Hello Friends!, today we are going to learn about CommandLineRunner
Interface.
Spring Boot provides two interfaces, CommandLineRunner
and ApplicationRunner
, to run specific pieces of code when an application is fully started. These interfaces get called just before run()
once SpringApplication
completes.
how to create Spring Boot Application using the below ways.
Application with Eclipse and Maven manually
CommandLineRunner Interface
Example implementation of CommandLineRunner
interface as follow.
you can implement CommandLineRunner
interface as follow, run()
gives access to the raw String array of incoming main method arguments.
package com.gyanideveloper.springboot.hook.command;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
@Component
public class CommandRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandRunner.class);
@Override
public void run(String... args) throws Exception {
logger.info("Application started with CommandLineRunner::run(-) ::" + Arrays.toString(args));
}
}
CommandLineRunner
interface provides single run()
, it gives access to the raw String array of incoming main method arguments.
If you need access to
ApplicationArguments
instead of the raw String array consider usingApplicationRunner
.
if you run the application, you can see application console log as follows, we have provides --server.port=8383
arguments while starting the application.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.0.RELEASE)
2020-06-01 19:16:41.705 INFO 10764 --- [ main] c.g.s.hook.SpringBootHookApplication : Starting SpringBootHookApplication on DESKTOP-R5VMSGQ with PID 10764 (F:\workspace-spring-tool-suite-4-4.6.1.RELEASE\spring-boot-hook\target\classes started by Gyani in F:\workspace-spring-tool-suite-4-4.6.1.RELEASE\spring-boot-hook)
2020-06-01 19:16:41.708 INFO 10764 --- [ main] c.g.s.hook.SpringBootHookApplication : No active profile set, falling back to default profiles: default
2020-06-01 19:16:42.331 INFO 10764 --- [ main] c.g.s.hook.SpringBootHookApplication : Started SpringBootHookApplication in 0.988 seconds (JVM running for 1.509)
2020-06-01 19:16:42.334 INFO 10764 --- [ main] c.g.s.hook.command.CommandRunner : Application started with CommandLineRunner::run(-) ::[--server.port=8383]
Ordering CommandLineRunner Interface
we can use multiple CommandLineRunner
or ApplicationRunner
beans in a single application. Spring automatically picks them up, in case of if we want to Order them, we can use the following two ways.
- Using
@Order
annotation - implementing
Ordered
interface
Using @Order
annotation
@Order
annotation defines the sort order for an annotated component.we can provide the priority for a component.
Let’s see the example code for more clarity.
package com.gyanideveloper.springboot.hook.command;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(1)
public class CommandRunner implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(CommandRunner.class);
@Override
public void run(String... args) throws Exception {
logger.info("Application started with CommandLineRunner::run(-) ::" + Arrays.toString(args));
}
}
implementing Ordered
interface
we need to implement Ordered
interface and provide the implementation for getOrder()
method
Let’s see the example code for more clarity.
package com.gyanideveloper.springboot.hook.command;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
@Component
public class CommandRunnerSecond implements CommandLineRunner, Ordered {
private static final Logger logger = LoggerFactory.getLogger(CommandRunnerSecond.class);
@Override
public void run(String... args) throws Exception {
logger.info("Application started with CommandRunnerSecond::run(-) ::" + Arrays.toString(args));
}
@Override
public int getOrder() {
return 2;
}
}
if you run the application, you can see application console log as follows, we have provides --server.port=8383
arguments while starting the application.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.3.0.RELEASE)
2020-06-01 22:25:59.148 INFO 9664 --- [ main] c.g.s.hook.SpringBootHookApplication : Starting SpringBootHookApplication on DESKTOP-R5VMSGQ with PID 9664 (F:\workspace-spring-tool-suite-4-4.6.1.RELEASE\spring-boot-hook\target\classes started by Sada in F:\workspace-spring-tool-suite-4-4.6.1.RELEASE\spring-boot-hook)
2020-06-01 22:25:59.154 INFO 9664 --- [ main] c.g.s.hook.SpringBootHookApplication : No active profile set, falling back to default profiles: default
2020-06-01 22:25:59.911 INFO 9664 --- [ main] c.g.s.hook.SpringBootHookApplication : Started SpringBootHookApplication in 1.268 seconds (JVM running for 2.27)
2020-06-01 22:25:59.916 INFO 9664 --- [ main] c.g.s.hook.command.CommandRunner : Application started with CommandLineRunner::run(-) ::[--server.port=8383]
2020-06-01 22:25:59.917 INFO 9664 --- [ main] c.g.s.hook.command.CommandRunnerSecound : Application started with CommandRunnerSecond::run(-) ::[--server.port=8383]
CommandLineRunner as @Bean
Example implementation of CommandLineRunner
as @Bean
follow.
package com.gyanideveloper.springboot.hook;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringBootHookApplication {
private static final Logger logger = LoggerFactory.getLogger(SpringBootHookApplication.class);
@Bean
public CommandLineRunner getRunner() {
return args -> {
logger.info("Application started with @Bean CommandLineRunner::run(-) ::" + Arrays.toString(args));
};
}
public static void main(String[] args) {
SpringApplication.run(SpringBootHookApplication.class, args);
}
}
if you run the application, you can see application console log as follows, we have provides --server.port=8383
arguments while starting the application.
2020-06-01 22:49:54.240 INFO 10096 --- [ main] c.g.s.hook.SpringBootHookApplication : Application started with @Bean CommandLineRunner::run(-) ::[--server.port=8383]
When to use it
When you want to execute some piece of code exactly before the application startup completes.
Final Note
we have learn CommandLineRunner
interface in details. full example code is available on GitHub