当前位置: 首页 > 知识库问答 >
问题:

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

厍书
2023-03-14

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

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

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

迎宾服务。java(接口)

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

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

应用程序的等效Spring Boot版本。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?
    }
}

共有1个答案

法池暝
2023-03-14

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

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

并使用@Component注释实现命令行运行程序接口

    @Component
    public class MyRunner implements CommandLineRunner {

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

      }
   }

启用自动配置(EnableAutoConfiguration)将执行通常的SpringBoot魔术。

更新时间:

正如@jeton所言,最新的Springboot实现了一个直接的:

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

见72.2的文档

 类似资料:
  • 问题内容: 所有, 我已经用Java编写了基于命令行的PhoneBook应用程序。该应用程序基本上会询问用户的一些详细信息,例如姓名,年龄,地址和电话号码,并将它们存储在文件中。其他操作包括按名称,电话号码等查找电话簿。所有详细信息都通过控制台输入。 我正在尝试为我已实现的每个功能编写JUnit测试用例,但无法弄清楚如何将实现代码重定向到我的JUnit测试方法中的某个东西,当我的实际代码停止供用户

  • 如何为控制台窗体应用程序创建exe?

  • 在阅读了如何在控制台中隐藏日志消息源之后,我对这个命令的工作原理感到困惑。 什么是做什么?它似乎不能删除setTimeout? 还有其他方法做同样的事情吗?

  • 问题内容: 我正在用Java编写一个简单的控制台应用程序(80x24),是否有等效的gotoxy(x,y)? 问题答案: 如果要通过gotoxy(x,y)将光标重新定位在控制台上特定的位置,则通常可以使用VT100控制代码来执行此操作。参见http://www.termsys.demon.co.uk/vtansi.htm。 做类似的事情 它将光标移动到控制台上的位置10,10。

  • 我对Java FX应用程序线程有问题。下面是一个伪代码: 问题是睡眠发生在窗口中,该窗口显示在show Menu()中,show LoadingPic()根本没有显示,而在最后窗口中显示的是show Map()。 show LoadingPic中的场景有一个进度条,运行2秒,与Thread.sleep(2000)相同。 因此,似乎javafx应用程序线程块show LoadingPic()和sho

  • 问题内容: 我正在研究使用python开发控制台应用程序,该应用程序应该能够在Windows和Linux下运行。为此,我真的很想使用像curses这样的高级控制台库。但是,据我所知,curses在Windows上不可用。 我还有什么其他选择?不幸的是,在Windows下不能使用cygwin。 谢谢你的帮助! 问题答案: 有一个问题。我从未尝试过,但它可能满足您的需求。听起来好像并没有完全的curs