From the course: Spring Boot 2.0 Essential Training

CommandLineRunner interface

From the course: Spring Boot 2.0 Essential Training

Start my 1-month free trial

CommandLineRunner interface

- [Narrator] One of my favorite interfaces in the Spring Boot ecosystem, and one that is very seldom talked about, is the CommandLineRunner. Now the CommandLineRunner is a very simple and effective tool, when you need to do some set amount of work. We don't always need to bring in web or some other big processes in order to do a simple task. Sometimes we just want to do a body of work and we want it deployed as a task, but we want to use spring in the most efficient way possible. So this is where I feel that spring boot especially with CommandLineRunner really shines. Now you can run it as a standalone spring boot application or you can run it in another spring boot application, say a weather application or a spring batch application, such that on startup, it will execute. In addition, you can use this as an admin or a batch process as part of your application as alluded to or in standalone fashion. But that's really the key, the fact that it's like admin or batch operations is really what we're targeting. Now, if we take a look at the CommandLineRunner Interface, it provides access to the application arguments and oftentimes, when we want to write a command line process, that's really what we need. We need to be able to inject those arguments into our Java code and then execute based on those. It can run simple or complex set of tasks, it's really up to you. And as I mentioned, it can be used standalone or within a running application, because when you get access to it, springs application context is up and running. Then you build and run your task and that's all you have to do to implement it. It is a simple run method. Now the CommandLineRunner interface has a cousin and that's the ApplicationRunner Interface. And really it's a very close cousin because they work in almost the exact same manner. They run a single-run method as part of their operations. You have access to the application arguments themselves with the ApplicationRunner just like with the CommandLineRunner. Now with spring, there's almost always a way to order behavior and CommandLineRunner or ApplicationRunners are no different. One thing to note is that you can put more than one of these into a given spring package. And when you do that, you need to control that order and that's really where the power comes. So you can use the @order annotation to determine which ones go first which ones go last by simply applying an order. So what you can do is if you have multiple tasks that must be executed in a single operation, you can put them into a single package, deploy them together and allow spring to control the order of operations. Kind of like when you're in bash and you pipe commands together, you're getting the same sort of structure with the CommandLineRunner and the @Order annotation.

Contents