Spring Boot中非Web应用程序

6

我刚开始学习Spring-Boot。我使用gradle创建了一个简单的Spring-Boot Web应用程序。但是我想要一个非Web应用程序项目,我可以从PSVM运行所有代码,并且还需要删除Tomcat依赖项。我搜索了一下,但没有找到例子。

如何使Spring Boot应用程序上下文作为非Web应用程序上下文启动?


1
这不是一个请求推荐的问题。虽然提问者对于在SO上如何提供答案存在误解,但这个问题是真实存在的。编辑以澄清。 - Andrew Spencer
尽管这是 https://dev59.com/U5nga4cB1Zd3GeqPedc- 的重复内容。 - Andrew Spencer
1个回答

13

Spring Boot提供了一些示例应用程序:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples。简单的命令行示例是这个:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-simple

从参考的示例项目中,关键是主类实现CommandLineRunner接口 - 这是来自示例项目的代码:

@SpringBootApplication
public class SampleSimpleApplication implements CommandLineRunner {
    // ...

    @Override
    public void run(String... args) {
        // ...
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(SampleSimpleApplication.class, args);
    }

}

我现在有答案了。SpringBoot提供了一个名为“CommandLineRunner”的接口。我们可以扩展它并覆盖其“run”方法来定义我们的类以执行。 - rishi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接