46

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 public static void main(String[] args) method, or have the main application class implement the CommandLineRunner interface and place the code in the run(String... args) method?

I will use an example as the context. Say I have the following [rudimentary] application (coded to interfaces, Spring style):

Application.java

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));
  }
}

GreeterService.java (interface)

public interface GreeterService {
  String greet(String[] tokens);
}

GreeterServiceImpl.java (implementation class)

@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() > 0 ? message.toString() : defaultMessage;
  }
}

The equivalent Spring Boot version of Application.java would be something along the lines: GreeterServiceImpl.java (implementation class)

@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?
    }
}
8
  • 3
    Use a CommandLineRunner that is what is it designed for, you can also create an instance of it and have dependencies injected into it. This would fail in your first scenario as you would not have a dependency injected or wouldn't be able to access it from the static method.
    – M. Deinum
    Jan 28, 2015 at 19:27
  • @M.Deinum so such console-based applications must implement the CommandLineRunner interface as a general rule of thumb? That provides sufficient clarity for me, because even the docs don't focus on this point (probably because it is implicit to most folks!).
    – Web User
    Jan 28, 2015 at 19:46
  • 3
    Well you ideally create a bean which implements this interface (and not your Application class). It has to do with separation of concerns (starting your application and executing your logic).
    – M. Deinum
    Jan 28, 2015 at 19:54
  • Are you referring to the GreeterService [implementation] bean? Because my Application class only uses this service. So there is a separation of concern at this point already, no?
    – Web User
    Jan 28, 2015 at 19:58
  • No I'm not referring to that. You are, for some reading, keep mentioning that you want your Application class to implement the CommandLineRunner interface. You probably don't want this but do that in a separate class instead.
    – M. Deinum
    Jan 28, 2015 at 20:04

1 Answer 1

62

You should have a standard loader:

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

and implement a CommandLineRunner interface with @Component annotation

    @Component
    public class MyRunner implements CommandLineRunner {

       @Override    
       public void run(String... args) throws Exception {

      }
   }

@EnableAutoConfiguration will do the usual SpringBoot magic.

UPDATE:

As @jeton suggests the latest Springboot implements a straight:

spring.main.web-application-type=none
spring.main.banner-mode=off

See docs at 72.2

6
  • 5
    Why do you think this is the proper way to implement a CLI application using Spring Boot? CommandLineRunner and ApplicationRunner are not only meant to be used for CLI apps. Plus, when you use either of them, Spring executes its respective run method before starting the main method of your app as evident by the log message INFO 6961 --- [ restartedMain] org.behrang.rpn.Main : Started Main in XX.YYY seconds (JVM running for MM.NNN) appearing after the code in your CommandLineRunner.run(...) method is executed.
    – Behrang
    Feb 27, 2016 at 17:03
  • 4
    @Behrang So what's the proper way to do it then?
    – Sam
    Oct 22, 2016 at 2:57
  • 1
    @Wesam Unfortunately there's not an elegant way to do this that I know of. Here's a possible solution: gist.github.com/behrangsa/6a7f050e7f6d193918ad7e910812fb28
    – Behrang
    Oct 24, 2016 at 9:52
  • 2
    @Behrang this answer gives the correct way to implement this functionality. I've just suggested an edit to add a link to the official sample: github.com/spring-projects/spring-boot/tree/v1.4.2.RELEASE/…
    – mdjnewman
    Nov 14, 2016 at 10:59
  • 2
    In springboot 2 it is spring.main.web-application-type=none
    – Zafar
    Feb 19, 2021 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.