title: How does a Spring Boot console based application work?
body: <p>If I am developing a rather simple Spring Boot console-based application, I am unsure about the placement of the main execution code. Should I place it in the <strong><code>public static void main(String[] args)</code></strong> method, or have the main application class implement the <strong><code>CommandLineRunner</code></strong> interface and place the code in the <strong><code>run(String... args)</code></strong> method?</p>
  
  <p>I will use an example as the context. Say I have the following [rudimentary] application (<em>coded to interfaces, Spring style</em>):</p>
  
  <p><strong>Application.java</strong>
  </p>
  
  <pre><code>public class Application {
  
    @Autowired
    private GreeterService greeterService;
  
    public static void main(String[] args) {
      // ******
      // *** Where do I place the following line of code
      // *** in a Spring Boot version of this application?
      // ******
      System.out.println(greeterService.greet(args));
    }
  }
  </code></pre>
  
  <p><strong>GreeterService.java</strong> (interface)
  </p>
  
  <pre><code>public interface GreeterService {
    String greet(String[] tokens);
  }
  </code></pre>
  
  <p><strong>GreeterServiceImpl.java</strong> (implementation class)
  </p>
  
  <pre><code>@Service
  public class GreeterServiceImpl implements GreeterService {
    public String greet(String[] tokens) {
  
      String defaultMessage = "hello world";
  
      if (args == null || args.length == 0) {
        return defaultMessage;
      }
  
      StringBuilder message = new StringBuilder();
      for (String token : tokens) {
        if (token == null) continue;
        message.append(token).append('-');
      }
  
      return message.length() &gt; 0 ? message.toString() : defaultMessage;
    }
  }
  </code></pre>
  
  <p>The equivalent Spring Boot version of <strong><code>Application.java</code></strong> would be something along the lines:
  <strong>GreeterServiceImpl.java</strong> (implementation class)
  </p>
  
  <pre><code>@EnableAutoConfiguration
  public class Application
      // *** Should I bother to implement this interface for this simple app?
      implements CommandLineRunner {
  
      @Autowired
      private GreeterService greeterService;
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
          System.out.println(greeterService.greet(args)); // here?
      }
  
      // Only if I implement the CommandLineRunner interface...
      public void run(String... args) throws Exception {
          System.out.println(greeterService.greet(args)); // or here?
      }
  }
  </code></pre>