Spring Boot Without A Web Server – 没有网络服务器的Spring Boot

最后修改: 2020年 9月 20日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

Spring Boot is a great framework for quickly creating new Java applications for a variety of use cases. One of the most popular uses is as a web server, using one of the many supported embedded servlet containers and template engines.

Spring Boot是一个伟大的框架,可以为各种使用情况快速创建新的Java应用程序。最受欢迎的用途之一是作为Web服务器,使用许多支持的嵌入式Servlet容器和模板引擎之一。

However, Spring Boot has a number of uses that do not require a web server: console applications, job scheduling, batch or stream processing, serverless applications, and more.

然而,Spring Boot有许多用途,不需要Web服务器控制台应用程序、作业调度、批处理或流处理、无服务器应用程序等等。

In this tutorial, we’ll look at several different ways to use Spring Boot without a web server.

在本教程中,我们将研究在没有Web服务器的情况下使用Spring Boot的几种不同方法。

2. Using Dependencies

2.使用依赖关系

The easiest way to prevent a Spring Boot application from starting an embedded web server is to not include the web server starter in our dependencies.

防止Spring Boot应用程序启动嵌入式Web服务器的最简单方法是不在我们的依赖中包括Web服务器启动器

This means not including the spring-boot-starter-web dependency in either the Maven POM or Gradle build file. Instead, we would want to use the more basic spring-boot-starter dependency in its place.

这意味着在Maven POM或Gradle构建文件中都不包括spring-boot-starter-web依赖项。相反,我们要用更基本的spring-boot-starter依赖来代替它。

Keep in mind it’s possible for Tomcat dependencies to be included in our application as transitive dependencies. In this case, we might need to exclude the Tomcat library from whichever dependency is including it.

请记住Tomcat的依赖关系有可能被包含在我们的应用程序中,作为过渡性依赖关系。在这种情况下,我们可能需要将Tomcat库从包括它的任何依赖中排除。

3. Modifying the Spring Application

3.修改Spring应用程序

Another way to disable the embedded web server in Spring Boot is by using code. We can use either the SpringApplicationBuilder:

另一种禁用Spring Boot中的嵌入式Web服务器的方法是使用代码。我们可以使用SpringApplicationBuilder

new SpringApplicationBuilder(MainApplication.class)
  .web(WebApplicationType.NONE)
  .run(args);

Or we can use the reference to the SpringApplication:

或者我们可以使用对SpringApplication的引用。

SpringApplication application = new SpringApplication(MainApplication.class);
application.setWebApplicationType(WebApplicationType.NONE);
application.run(args);

In either case, we have the benefit of keeping the servlet and container APIs available on the classpath. This means we can still use the web server libraries without starting the web server. This can be useful, for example, if we want to use them to write tests or use their APIs in our own code.

在这两种情况下,我们的好处是在classpath上保持Servlet和容器API的可用性。这意味着我们仍然可以在不启动Web服务器的情况下使用Web服务器库。例如,如果我们想用它们来编写测试或在我们自己的代码中使用它们的API,这可能很有用。

4. Using Application Properties

4.使用应用程序属性

Using code to disable the web server is a static option — it will affect our application no matter where we deploy it. But what if we want to create the web server in specific circumstances?

使用代码来禁用Web服务器是一个静态选项–无论我们在哪里部署,它都会影响我们的应用程序。但如果我们想在特定情况下创建Web服务器呢?

In this case, we can use Spring application properties:

在这种情况下,我们可以使用Spring应用程序属性。

spring.main.web-application-type=none

Or using the equivalent YAML:

或者使用等价的YAML。

spring:
  main:
    web-application-type: none

The benefit of this approach is we can conditionally enable the web server. Using Spring profiles or conditionals, we can control the web server behavior in different deployments.

这种方法的好处是我们可以有条件地启用 Web 服务器。使用Spring profilesconditionals,我们可以控制不同部署中的Web服务器行为。

For example, we could have the web server running in development only to expose metrics or other Spring endpoints while keeping it disabled in production for security reasons.

例如,我们可以让Web服务器在开发中运行,只用于暴露指标或其他Spring端点,而在生产中出于安全原因保持其禁用。

Note that some earlier versions of Spring Boot used a boolean property named web-environment to enable and disable the web server. With the adoption of both traditional and reactive containers in Spring Boot, the property has been renamed and now uses an enum.

请注意,Spring Boot的一些早期版本使用名为web-environmentboolean属性来启用和禁用Web服务器。随着Spring Boot中传统容器和反应式容器的采用,该属性已被重新命名,现在使用一个枚举

5. Conclusion

5.总结

There are many reasons for creating Spring Boot applications without a web server. In this tutorial, we’ve seen multiple ways to do this. Each has its own pros and cons, so we should pick the approach that best meets our needs.

在没有Web服务器的情况下创建Spring Boot应用程序有很多原因。在本教程中,我们已经看到了多种方法来做到这一点。每种方法都有自己的优点和缺点,所以我们应该选择最符合我们需求的方法。