是否可以显式配置Spring Boot服务器?

时间:2018-05-24 09:58:50

标签: spring-boot spring-boot-starter

我有一个Spring Boot应用程序,其中一个依赖项使用spring和一个嵌入式jetty来启动ad-hoc Web服务器。这导致我的spring启动应用程序以jetty而不是tomcat启动。

我的spring-boot-starter-web:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
  <exclusions>
    <exclusion>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-websocket</artifactId>
    </exclusion>
  </exclusions>
</dependency>

依赖关系pom:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.integration</groupId>
  <artifactId>spring-integration-http</artifactId>
</dependency>

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
</dependency>

<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-server</artifactId>
</dependency>

<dependency>
  <groupId>org.eclipse.jetty</groupId>
  <artifactId>jetty-servlet</artifactId>
</dependency>

是否有可能将服务器配置为由spring boot显式使用而不是由依赖关系树推断?

修改

我进一步调查了这个问题并创建了一个repo来重现这个问题:github.com/svettwer/spring-server-test
org.eclipse.jetty.websocket:javax-websocket-server-impl导致spring从jetty开始,不需要任何其他配置。

编辑2

Spring Boot 2.x中不再出现此问题

编辑3 我将删除前面提到的repo,但这里是导致该问题的依赖项设置:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.5.7.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>1.5.7.RELEASE</version>
        <scope>test</scope>
    </dependency>

    <!-- Comment that in to start spring with jetty-->
    <dependency>
        <groupId>org.eclipse.jetty.websocket</groupId>
        <artifactId>javax-websocket-server-impl</artifactId>
        <version>9.4.8.v20171121</version>
    </dependency>

</dependencies>

2 个答案:

答案 0 :(得分:0)

本指南可能会对您有所帮助:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html

如果您运行mvn dependency:tree并搜索jetty,您可能会发现需要将其排除,例如:

spring-boot-starter-jetty

排除在示例中:

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- Exclude the Tomcat dependency -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<!-- Use Jetty instead -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

这有利于码头上的tomcat - 但你有希望得到这个想法......

希望这有帮助。

答案 1 :(得分:0)

通常,如果类路径上有spring-boot-starter-tomcat(通过spring-boot-starter-web),则应始终选择Tomcat,因为它优先于其他servlet容器。即使您有以下依赖项,Spring启动也将从Tomcat开始:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-server</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-webapp</artifactId>
</dependency>
<dependency>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-servlet</artifactId>
</dependency>

您可以通过注册自己的ServletWebServerFactory以编程方式覆盖所选的servlet容器,例如:

@Bean
public ServletWebServerFactory factory() {
    return new TomcatServletWebServerFactory();
}

您可以选择预定义的TomcatServletWebServerFactoryJettyServletWebServerFactoryUndertowServletWebServerFactory

相关问题