没有 Web 服务器的 Spring Boot

新手上路,请多包涵

我有一个简单的 Spring Boot 应用程序,它从 JMS 队列中获取消息并将一些数据保存到日志文件中,但不需要 Web 服务器。有没有办法在没有 Web 服务器的情况下启动 Spring Boot?

原文由 Michael 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 616
2 个回答

如果类路径上没有 Tomcat 依赖项,Spring boot 将不包含嵌入式 tomcat。您可以在 EmbeddedServletContainerAutoConfiguration 课程中亲自查看这一事实,您可以在 此处 找到其来源。

代码的核心是使用 @ConditionalOnClass 类上的注释 EmbeddedTomcat


此外,有关更多信息,请查看 指南和 指南以及文档的 这一 部分

原文由 geoand 发布,翻译遵循 CC BY-SA 3.0 许可协议

如果你想在没有 servlet 容器的情况下运行 Spring Boot 1.x ,但在类路径上有一个(例如用于测试),请使用以下内容,如 spring boot 文档 中所述:

 @Configuration
@EnableAutoConfiguration
public class MyClass {
    public static void main(String[] args) throws JAXBException {
         SpringApplication app = new SpringApplication(MyClass.class);
         app.setWebEnvironment(false); //<<<<<<<<<
         ConfigurableApplicationContext ctx = app.run(args);
    }
}

另外,我只是偶然发现了这个属性:

 spring.main.web-environment=false

原文由 Stefan K. 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题