本文介绍了基于Spring Boot控制台的应用程序如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

I will use a 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 (接口)

GreeterService.java (interface)

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

GreeterServiceImpl.java (实现类)

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

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

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


推荐答案

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

You should have a standard loader:

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

并实现 CommandLineRunner 接口与 @Component 注释

    @Component
    public class MyRunner implements CommandLineRunner {

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

      }
   }

@EnableAutoConfiguration 将执行通常的SpringBoot魔术。

@EnableAutoConfiguration will do the usual SpringBoot magic.

更新:

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

As @jeton suggests the laters Springboot implements a straight:

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

参见

这篇关于基于Spring Boot控制台的应用程序如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:40