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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28199999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:46:09  来源:igfitidea点击:

How does a Spring Boot console based application work?

javaconsole-applicationspring-boot

提问by Web User

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 CommandLineRunnerinterface and place the code in the run(String... args)method?

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

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

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

Application.java

应用程序.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)

GreeterService.java(接口)

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

GreeterServiceImpl.java(implementation class)

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

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

等效的 Spring Boot 版本Application.java是这样的: 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?
    }
}

回答by lrkwz

You should have a standard loader:

你应该有一个标准的加载器:

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

and implement a CommandLineRunnerinterface with @Componentannotation

并实现CommandLineRunner带有@Component注解的接口

    @Component
    public class MyRunner implements CommandLineRunner {

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

      }
   }

@EnableAutoConfigurationwill do the usual SpringBoot magic.

@EnableAutoConfiguration会做通常的 SpringBoot 魔术。

UPDATE:

更新:

As @jeton suggests the laters Springboot implements a straight:

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

spring.main.web-environment=false
spring.main.banner-mode=off

See docs at 72.2

请参阅72.2 处的文档