没有Web服务器的Spring Boot

Mic*_*ael 85 java spring-boot

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

Ste*_* K. 125

如果你想在没有servlet容器的情况下运行spring boot,但是在类路径上运行一个(例如用于测试),请使用以下命令,如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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

spring.main.web-environment=false
Run Code Online (Sandbox Code Playgroud)

  • 只需将属性添加到`application.properties`就可以了. (11认同)
  • 这有效但在Spring Boot 2.0中已弃用.请参阅2.0版本的答案:/sf/answers/3107601381/ (7认同)
  • 这些天你应该使用的是 `spring.main.web-application-type=none` (5认同)
  • 该物业是它所需要的.很好,谢谢. (2认同)
  • 启动 2.x - application.setWebApplicationType(WebApplicationType.NONE); (2认同)

wil*_*oop 65

Spring Boot 2.x


其中WebApplicationType:

  • NONE - 应用程序不应作为Web应用程序运行,也不应启动嵌入式Web服务器.
  • REACTIVE - 应用程序应作为响应式Web应用程序运行,并应启动嵌入式响应式Web服务器.
  • SERVLET - 应用程序应作为基于servlet的Web应用程序运行,并应启动嵌入式servlet Web服务器.


san*_*o21 52

你可以创建这样的东西:

@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(false).run(args);
  }
}
Run Code Online (Sandbox Code Playgroud)

@Component
public class CommandLiner implements CommandLineRunner {

  @Override
  public void run(String... args) throws Exception {
    // Put your logic here
  }

}
Run Code Online (Sandbox Code Playgroud)

依赖仍然存在,但没有使用.


geo*_*and 20

如果您没有类路径上的Tomcat依赖项,则Spring引导将不包括嵌入式tomcat.你可以在这里EmbeddedServletContainerAutoConfiguration可以找到源代码的类中查看这个事实.

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


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


小智 11

对于 Spring boot v2.1.3.RELEASE,只需将以下属性添加到 application.propertes 中:

spring.main.web-application-type=none
Run Code Online (Sandbox Code Playgroud)


小智 11

使用此代码。

SpringApplication application = new SpringApplication(DemoApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);
Run Code Online (Sandbox Code Playgroud)


Meh*_*hdi 8

最简单的解决方案。在您的application.properties文件中。添加上一个答案提到的以下属性:

spring.main.web-environment = false

对于Spring Boot Starter的2.0.0版本,请使用以下属性:

spring.main.web-application-type =无

有关所有属性的文档,请使用以下链接:https : //docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html


Suc*_*mar 6


小智 5

如果您的应用程序需要 Web 功能(例如org.springframework.web.client.RestTemplateREST 调用)但不想启动 TOMCAT 服务器,只需在 POM 中排除它:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)


ali*_*dro 5

Spring Boot 有很多启动器,有些启动器有嵌入式 Web 服务器,有些则没有。以下具有嵌入式 Web 服务器:

spring-boot-starter-web
spring-boot-starter-data-jpa
spring-boot-starter-jetty
spring-boot-starter-tomcat
spring-boot-starter-jdbc
spring-boot-starter-data-rest
...
Run Code Online (Sandbox Code Playgroud)

选择满足您的要求且没有服务器支持的一个。

我只需要在我的 spring 应用程序中发出restful json api 请求,所以我需要的启动器是

spring-boot-starter-json
Run Code Online (Sandbox Code Playgroud)

它提供RestTemplatejackson供我使用。