【问题标题】:ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory beanApplicationContextException:由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext
【发布时间】:2018-10-18 07:04:21
【问题描述】:

我使用 Spring Boot 编写了一个 Spring Batch 应用程序。当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行良好。但是,当我尝试在 linux 服务器上运行它时,它给了我以下异常

Unable to start web server; nested exception is
org.springframework.context.ApplicationContextException: 
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.

下面是我的运行方式:

java -cp jarFileName.jar; lib\* -Dlogging.level.org.springframework=DEBUG -Dspring.profiles.active=dev -Dspring.batch.job.names=abcBatchJob com.aa.bb.StartSpringBatch > somelogs.log

【问题讨论】:

标签: spring spring-boot spring-mvc


【解决方案1】:

案例一:

您的 Spring Boot 入门类中缺少@SpringBootApplication 注释。

案例2:

对于非 Web 应用程序,请在属性文件中禁用 web application type

application.properties:

spring.main.web-application-type=none

如果您使用application.yml,则添加:

  spring:
    main:
      web-application-type: none

对于 Web 应用程序,在主类中扩展 *SpringBootServletInitializer*

@SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(YourAppliationName.class, args);
    }
}

案例3:

如果您使用spring-boot-starter-webflux,则还要添加spring-boot-starter-web 作为依赖项。

【讨论】:

  • 感谢回复,但我的应用程序不是网络应用程序
  • @VijayantBhatia 你明白了吗?我在 Spring Boot 中运行一个非 Web 应用程序并得到同样的错误。
  • 案例 4 怎么样。它通常安装在 Tomcat 上,但尝试在本地运行而不使用 tomcat 服务器?我尝试了 jetty,但它似乎没有帮助,我需要创建配置文件。
【解决方案2】:

您的 Spring Boot 入门课程中可能缺少 @SpringBootApplication

@SpringBootApplication
public class LoginSecurityAppApplication {

    public static void main(String[] args) {
        SpringApplication.run(LoginSecurityAppApplication.class, args);
    }

}

【讨论】:

    【解决方案3】:

    解决办法是:

    我在application.yml 文件中将以下属性显式设置为none

    spring:
      main:
        web-application-type: none
    

    【讨论】:

      【解决方案4】:

      我的解决方案与糟糕的依赖有关。我有:

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

      在我的 pom 中,我必须注释掉排除项才能使其正常工作。由于某种原因,它必须寻找这个 tomcat 包。

      【讨论】:

      • 这正是大多数情况下的问题所在。 Tomcat 被排除在外,因此应用程序可以在外部服务器上运行,如果应用程序没有扩展 SpringBootServletInitializer,则会发生这种情况。
      【解决方案5】:

      你可能会在你的项目中使用这个:

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-webflux</artifactId>
      </dependency>
      

      在这种情况下,您还必须添加:

      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      

      奇迹发生了:)

      PS:这是因为 Spring 默认使用 web-MVC 而不是 web-flux 当两者都可用时

      【讨论】:

        【解决方案6】:

        在我的情况下,问题解决了从 spring-boot-starte-web 中注释 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> -->
            </dependency> 
        

        【讨论】:

          【解决方案7】:

          添加以下 bean 对我有用。

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

          我使用SpringApplication.run(MyApplication.class, args); 没有@SpringBootApplication 注释运行非Web Spring 应用程序。

          【讨论】:

            【解决方案8】:

            注释类public static void main,例如:@SpringBootApplication

            【讨论】:

              【解决方案9】:

              我在迁移到 Spring Boot 时遇到了这个问题。我找到了advice to remove dependencies,它有所帮助。所以,我删除了对 jsp-api 项目的依赖。此外,还必须删除 servlet-api 依赖项。

              compileOnly group: 'javax.servlet.jsp', name: 'jsp-api', version: '2.2' 
              

              【讨论】:

              • 我在编译组中遇到了这个问题:'javax.servlet',名称:'javax.servlet-api',版本:'4.0.1'
              • 像@mger1979 我的问题是使用组:'javax.servlet',名称:'javax.servlet-api',版本:'3.1.0' 和 spring-boot:2.1.8.RELEASE所以我将“javax.servlet”更新为“4.0.1”版本,现在一切正常,谢谢。
              【解决方案10】:

              除了其他答案中可能的解决方案外,您的计算机上也可能发生了某种 Maven 依赖项损坏。我在尝试运行我的(Web)Spring 启动应用程序时遇到了同样的错误,它通过运行以下命令得到了解决 -

              mvn dependency:purge-local-repository -DreResolve=true
              

              紧随其后

              mvn package
              

              我找到了这个解决方案,调查了另一个问题,即 Eclipse 不允许我从 IDE 运行主应用程序类,原因是另一个错误,类似于这个 SO 线程上的错误 -> The type org.springframework.context.ConfigurableApplicationContext cannot be resolved. It is indirectly referenced from required .class files

              【讨论】:

              • mvn 依赖:purge-local-repository -DreResolve=true; mvn 清洁包;它帮助了我。
              【解决方案11】:

              类似于确保安装org.springframework.boot:spring-boot-starter-tomcat 的解决方案,我的build.gradle 中缺少org.eclipse.jetty:jetty-server

              org.springframework.boot:spring-boot-starter-web 需要一台服务器,无论是 Tomcat、Jetty 还是其他服务器——它可以编译但没有它就无法运行。

              【讨论】:

                【解决方案12】:

                如果您正在使用 IntelliJ 并且这种情况发生在您身上(就像它对我的菜鸟所做的那样),请确保运行设置具有 Spring Boot 应用程序而不是普通应用程序。

                【讨论】:

                  【解决方案13】:

                  就我而言,问题是我的 Eclipse 中没有单独安装 Tomcat 服务器。我假设我的 Springboot 会自动启动服务器。

                  由于我的主类扩展了SpringBootServletInitializer 并覆盖了configure 方法,我肯定需要在我的IDE 中安装一个Tomcat 服务器。

                  要安装,首先下载 Apache Tomcat(在我的例子中是版本 9)并使用服务器选项卡创建服务器。

                  安装后,在服务器上运行主类。

                  Run As -> Run on Server
                  

                  【讨论】:

                    【解决方案14】:

                    我尝试使用 Spring Boot 创建一个 Web 应用程序,但遇到了同样的错误。 检查后我发现我缺少一个依赖项。因此,请务必将以下依赖项添加到您的 pom.xml 文件中。

                        <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-web</artifactId>
                        </dependency> 
                    

                    【讨论】:

                      【解决方案15】:

                      我确实在 IntelliJ IDEA 中右键单击我的项目,然后在 Maven -> 重新加载项目,问题解决了。

                      【讨论】:

                        【解决方案16】:

                        我想运行 WAR 类型的 spring boot 应用程序,当我尝试将应用程序作为 spring boot 应用程序运行时,我遇到了错误。因此,在 application.properties 中声明 Web 应用程序类型对我有用。 spring.main.web-application-type=none

                        可能的网络应用类型:

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

                        【讨论】:

                          【解决方案17】:

                          我在使用 tomcat-jasper 较新版本时遇到了同样的错误

                          <dependency>
                              <groupId>org.apache.tomcat</groupId>
                              <artifactId>tomcat-jasper</artifactId>
                              <version>10.0.6</version>
                          </dependency>
                          

                          我用稳定的旧版本替换它对我来说很好。

                          <dependency>
                              <groupId>org.apache.tomcat</groupId>
                              <artifactId>tomcat-jasper</artifactId>
                              <version>9.0.46</version>
                          </dependency>
                          

                          【讨论】:

                            【解决方案18】:

                            缺少依赖可能是导致此问题的原因

                            <dependency>
                              <groupId>org.springframework.boot</groupId>
                              <artifactId>spring-boot-starter-web</artifactId>
                            </dependency>
                            

                            【讨论】:

                              【解决方案19】:

                              我在尝试将我的 Web 应用程序作为 fat jar 而不是从我的 IDE (IntelliJ) 中运行时遇到了这个问题。

                              这对我有用。只需将默认配置文件添加到 application.properties 文件。

                              spring.profiles.active=default
                              

                              如果您已经设置了其他特定配置文件 (dev/test/prod),则不必使用默认值。但是,如果您还没有,则需要将应用程序作为胖 jar 运行。

                              【讨论】:

                                【解决方案20】:

                                pom.xml 中的spring-boot-starter-parent 升级到最新版本已为我解决了这个问题。

                                【讨论】:

                                • 您能准确分享您添加的内容吗?哪个版本?
                                • 根据我发布此内容的时间,它看起来像是 2.0.2.RELEASE。 2.0.3 次日发布。
                                【解决方案21】:

                                至于我,我删除了 tomcat 依赖项中的 provided 范围。

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

                                【讨论】:

                                  【解决方案22】:

                                  就我而言,我使用的是 TOMCAT 8 并更新到 TOMCAT 9 修复了它:

                                    <modelVersion>4.0.0</modelVersion>
                                    <groupId>spring-boot-app</groupId>
                                    <artifactId>spring-boot-app</artifactId>
                                    <version>0.0.1-SNAPSHOT</version>
                                    <packaging>war</packaging>
                                  
                                    <parent>
                                      <groupId>org.springframework.boot</groupId>
                                      <artifactId>spring-boot-starter-parent</artifactId>
                                      <version>2.3.1.RELEASE</version>
                                      <relativePath/> <!-- lookup parent from repository -->
                                    </parent>
                                  
                                    <dependencies>
                                      <dependency>
                                        <groupId>org.springframework.boot</groupId>
                                        <artifactId>spring-boot-starter-web</artifactId>
                                      </dependency>
                                      <dependency>
                                        <groupId>org.springframework.boot</groupId>
                                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                                      </dependency>
                                      <dependency>
                                        <groupId>com.h2database</groupId>
                                        <artifactId>h2</artifactId>
                                      </dependency>
                                      <dependency>
                                        <groupId>org.springframework.boot</groupId>
                                        <artifactId>spring-boot-starter-test</artifactId>
                                      </dependency>
                                    </dependencies>
                                  
                                    <build>
                                      <plugins>
                                        <plugin>
                                          <groupId>org.codehaus.mojo</groupId>
                                          <artifactId>exec-maven-plugin</artifactId>
                                          <version>1.2.1</version>
                                          <executions>
                                            <execution>
                                              <goals>
                                                <goal>java</goal>
                                              </goals>
                                            </execution>
                                          </executions>
                                          <configuration>
                                            <mainClass>com.example.Application</mainClass>
                                          </configuration>
                                        </plugin>
                                      </plugins>
                                    </build>
                                  
                                    <properties>
                                      <tomcat.version>9.0.37</tomcat.version>
                                    </properties>
                                  

                                  相关问题:

                                  1. https://github.com/spring-projects/spring-boot/issues?q=missing+ServletWebServerFactory+bean
                                  2. https://github.com/spring-projects/spring-boot/issues/22013 - Spring Boot 应用作为一个模块
                                  3. https://github.com/spring-projects/spring-boot/issues/19141 - 当 spring-boot-starter-web 作为依赖项包含时,当主类扩展使用 @SpringBootApplication 注释的基类时,应用程序无法加载

                                  【讨论】:

                                    【解决方案23】:

                                    我的问题与原始问题中的问题相同,只是我是通过 Eclipse 而不是 cmd 运行的。尝试了列出的所有解决方案,但没有奏效。然而,对我来说最终的工作解决方案是通过 cmd 运行(或者可以通过 Eclipse 类似地运行)。使用了一个修改后的命令,附加了来自 cmd 的 spring 配置:

                                    start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>
                                    

                                    我想我的问题是没有正确加载弹簧配置。

                                    【讨论】:

                                      猜你喜欢
                                      • 2019-08-07
                                      • 2018-03-09
                                      • 2019-04-13
                                      • 2021-06-19
                                      • 1970-01-01
                                      • 2019-12-03
                                      • 2019-12-01
                                      • 2018-12-16
                                      相关资源
                                      最近更新 更多