Standalone Spring Boot console apps with CommandLineRunner

Spring Boot isn't just about cloud-native microservices development. You can use it to create standalone Java apps as well.

RESTful APIs get all the attention, but sometimes, you just need to auto wire a few JavaBeans into a simple Spring Boot console app.

How to create a standalone Spring Boot console app

So, how do you create a standalone console app in Spring Boot? Simply follow these five steps:

  1. Decorate your Java class with the @SpringBootApplication annotation.
  2. In the main method, instantiate the class within the context of a SpringApplication.
  3. Have the class implement the CommandLineRunner interface.
  4. Add the interface's required run(String… args) method.
  5. Write your standalone Spring Boot console app code inside the run method of the class.

Spring Boot's CommandLineRunner

In a typical Java application, to create a console app, a developer places code inside a class's main method.

However, in a Spring Boot app, the Spring context is not implicitly available inside main. If the Spring context isn't available, you can't auto wire resources, and you can't take advantage of Spring's inversion of control container.

That's where the CommandLineRunner interface comes in.

If your Spring Boot application class implements Spring's CommandLineRunner interface, you can place all the code you normally write in a main method inside of the interface's run method.

Autowiring Spring Boot app components

Furthermore, since the run method executes within the context of your Spring Boot app, you can access any autowired resource, such as the following:

  • @Component-decorated JavaBeans.
  • RestTemplates to invoke GET and POST operations.
  • Transactional Hibernate and JPA classes.
  • Java Database Connectivity (JDBC) templates and Spring Data repository objects.
  • EntityManagers and DataSources.
  • WebClients and HttpClients that access RESTful Spring APIs.

Standalone Spring Boot app example

The following example of a standalone Spring Boot console app uses dependency injection to auto wire a @Component-annotated JavaBean into the interface's run method.

@SpringBootApplication
public class SpringConsoleApp implements CommandLineRunner {

       @Autowired
       Score score;

       public static void main(String[] args) {
              SpringApplication.run(SpringConsoleApp.class, args);
       }

       public void run(String... args) throws Exception {
              score.wins++;
              System.out.println(score.wins);
       }
}

@Component
class Score {
       int wins, losses, ties;
}

Advanced Spring Boot app development

This is a simple Spring Boot console app, but a Spring developer can iteratively and incrementally enhance this app to access RESTful APIs or invoke JDBC templates. Simply add the appropriate Spring Boot starter to the Maven or Gradle build file, and call the REST and JDBC APIs as necessary.

Spring Boot development isn't just about microservices and REST. You can use the framework to create powerful, standalone Spring Boot applications as well.

Cameron McKenzie has been a Java EE software engineer for 20 years. His current specialties include Agile development; DevOps; and container-based technologies such as Docker, Swarm and Kubernetes.

View All Videos