4 回答

TA贡献1796条经验 获得超10个赞
我遵循以下方法,
@SpringBootApplication
public class Test {
public static void main(String[] args) {
SpringApplication.run(Test.class, args);
}
}
@Component
public class MyRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Enter word!");
Scanner scanner = new Scanner(System.in);
String line = scanner.nextLine();
System.out.println(line);
}
}
它对我很有用,你也可以在主类中组合命令行监听器。

TA贡献1810条经验 获得超4个赞
Spring Boot 不是为任何类型的通用用途而设计的。它有特定的目的。我们不应该总是考虑如何使用 Spring Boot 来满足任何一种需求。我知道 Spring Boot 已经变得非常流行。对于您的问题,如果您想创建交互式应用程序/程序,则必须以简单的方式进行。我只是在代码片段下方提供。
public class Interactive
{
public static void main (String[] args)
{
// create a scanner so we can read the command-line input
Scanner scanner = new Scanner(System.in);
System.out.print("Enter your name ? ");
String username = scanner.next();
System.out.print("Enter your age ? ");
int age = scanner.nextInt();
//Print all name, age etc
}
}
希望这可能是您的要求。同样,Spring Boot 应用程序不能说是真正意义上的交互式。那里有很多解释。Spring Boot 适用于客户端服务器架构。

TA贡献1966条经验 获得超4个赞
还是 Spring Boot 不适合我想做的事情?
你是对的。
Spring Boot 应用程序是一个服务器应用程序(与任何其他 .war 一样)并在嵌入式 Tomcat 中运行。什么样的扫描仪在这里可以?
如果你想与之交互——你应该创建 REST 控制器并通过端点发送/接收数据。

TA贡献1772条经验 获得超8个赞
所以我发现了一种通过尝试普通的 gradle 应用程序(不是 spring boot)来使其工作的方法。我遇到了同样的问题,解决方案是在我的build.gradle文件中添加一行以连接默认值stdin
在普通的 java gradle 应用程序中:
run {
standardInput = System.in
}
但在 Spring Boot 应用程序中,我添加了以下行:
bootRun {
standardInput = System.in
}
事实证明,Spring Boot 毕竟可以处理命令行应用程序。
添加回答
举报