【问题标题】:How to exclude embeded Tomcat in Spring Boot application如何在 Spring Boot 应用程序中排除嵌入式 Tomcat
【发布时间】:2018-05-29 09:09:24
【问题描述】:

我们如何从 Spring Boot 应用程序中排除嵌入式 Tomcat 服务器,以便我们可以在 JBoss 服务器上运行该 jar?

【问题讨论】:

  • 你不需要排除它,它只会在服务器上运行并且是可执行的。
  • 好吧,这意味着即使我们在生产服务器上运行 jar,在我们的例子中,我们在 prod 上使用 Jboss,会自动部署在 jboss 而不是 Tomcat 上?
  • 我假设的是 war 文件,而不是 jar 文件。您至少应该将依赖项设置为provided,以便将它们移动到不同的目录。该目录由 Spring Boot 使用,但在部署文件时不使用。
  • 如果是 jar 文件,它会做什么,是部署在嵌入式 Tomcat 上还是部署在 jboss 上。

标签: spring-boot


【解决方案1】:

您可以在 pom 文件中排除:

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

你可以通过截图关注这个link

Spring documentation on Embedded servlet container

【讨论】:

  • 当我使用上述建议运行我的应用程序时,它给了我错误“在关机时取消注册 JMX 暴露的 bean”
【解决方案2】:

您可以按如下方式修改您的 POM.xml 文件:

  <!-- Removing the dependency for the embedded tomcat -->
            <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>

【讨论】:

    【解决方案3】:

    添加 &lt;exclusions&gt;&lt;artificatId&gt; 相关的标签作为 'spring-boot-starter-web'

    <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
    

    您的最终依赖项应如下所示:

    <dependencies>
            <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>
    </dependencies>
    

    【讨论】:

      【解决方案4】:

      另一种方法是在您的pom.xml 中将tomcat 依赖项的scope 标记为provided

          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-web</artifactId>
          </dependency>
      
          <dependency>
              <groupId>org.apache.tomcat.embed</groupId>
              <artifactId>tomcat-embed-websocket</artifactId>
              <scope>provided</scope>
          </dependency>
      
          <dependency>
              <groupId>org.apache.tomcat.embed</groupId>
              <artifactId>tomcat-embed-core</artifactId>
              <scope>provided</scope>
          </dependency>
      

      【讨论】:

        【解决方案5】:

        最好提一下

        pom.xml 文件中提供的范围

        在基于 Spring Boot maven 的应用程序中排除嵌入式 tomcat 服务器。

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        

        【讨论】:

          猜你喜欢
          • 2020-07-18
          • 2017-10-21
          • 1970-01-01
          • 1970-01-01
          • 2016-10-09
          • 2015-07-31
          • 2016-05-02
          • 1970-01-01
          • 2020-11-09
          相关资源
          最近更新 更多