0 votes
131 views
in Spring Boot by

What is the difference between CommandLineRunner and ApplicationRunner in Spring Boot ?

1 Answer

0 votes
by (2.8k points)

See the bellow details about Spring Boot CommansLineRunner and ApplicationRunner:- 

CommandLineRunner and ApplicationRunner are functional interface in spring boot.

CommandLineRunner and ApplicationRunner interfaces hold run(), which will be call just after SpringBeans and ApplicationContext are created.

CommandLineRunner:- CommandLineRunner will execute run() method, just after applicationcontext is created and before springboot application starts up.

It accepts the argument, which are passed at time of server startup.

@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 will execute the run() method, just after applicationcontext is created and before springboot application startup.

ApplicationRunner has run() method with argument ApplicationArgument. It has convenient methods like getOptionNames(), getOptionValues() and getSourceArgs().

@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());

    }

}

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

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...