基于Spring Boot控制台的应用程序如何工作?

Web*_*ser 37 java console-application spring-boot

如果我正在开发一个相当简单的基于Spring Boot控制台的应用程序,我不确定主执行代码的位置.我应该将它放在public static void main(String[] args)方法中,还是让主应用程序类实现CommandLineRunner接口并将代码放在run(String... args)方法中?

我将使用一个例子作为上下文.假设我有以下[基本]应用程序(编码为接口,Spring样式):

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));
  }
}
Run Code Online (Sandbox Code Playgroud)

GreeterService.java(界面)

public interface GreeterService {
  String greet(String[] tokens);
}
Run Code Online (Sandbox Code Playgroud)

GreeterServiceImpl.java(实现类)

@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;
  }
}
Run Code Online (Sandbox Code Playgroud)

等效的Spring Boot版本Application.java就是这样的: GreeterServiceImpl.java(实现类)

@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?
    }
}
Run Code Online (Sandbox Code Playgroud)

lrk*_*kwz 50

你应该有一个标准装载机:

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

并实现CommandLineRunner@Component注释的接口

    @Component
    public class MyRunner implements CommandLineRunner {

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

      }
   }
Run Code Online (Sandbox Code Playgroud)

@EnableAutoConfiguration 会做通常的SpringBoot魔术.

更新:

正如@jeton建议的那样,Springboot实现了一个直线:

spring.main.web-environment=false
spring.main.banner-mode=off
Run Code Online (Sandbox Code Playgroud)

72.2的文档

  • 为什么您认为这是使用Spring Boot实现CLI应用程序的正确方法?`CommandLineRunner`和`ApplicationRunner`不仅仅用于CLI应用程序.另外,当你使用它们中的任何一个时,Spring会在启动应用程序的`main`方法之前执行其各自的`run`方法,正如日志消息`INFO 6961 --- [restartedMain] org.behrang.rpn.Main所示:在执行`CommandLineRunner.run(...)`方法中的代码后出现XX.YYY秒(运行MM.NNN的JVM). (4认同)
  • @Behrang那么这样做的正确方法是什么? (4认同)
  • @Behrang这个答案提供了实现此功能的正确方法.我刚刚建议编辑添加官方样本的链接:https://github.com/spring-projects/spring-boot/tree/v1.4.2.RELEASE/spring-boot-samples/spring-boot-样本简单 (2认同)
  • 在 springboot 2 中,它是 `spring.main.web-application-type=none` (2认同)