Spring ApplicationContextException:由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext

Spring ApplicationContextException:由于缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext,spring,spring-boot,spring-mvc,Spring,Spring Boot,Spring Mvc,我已经使用SpringBoot编写了一个spring批处理应用程序。当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行良好。然而,当我试图在linux服务器上运行它时,它给了我以下异常 Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationCo

我已经使用SpringBoot编写了一个spring批处理应用程序。当我尝试在本地系统上使用命令行和类路径运行该应用程序时,它运行良好。然而,当我试图在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
案例1: spring boot starter类中缺少
@SpringBootApplication
注释

@SpringBootApplication
public class LoginSecurityAppApplication {

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

}
案例2: 对于非web应用程序,在属性文件中禁用
web应用程序类型
: 在
application.properties
中:

spring.main.web-application-type=none
如果使用
application.yml
,则添加:

  spring:
    main:
      web-application-type: none
对于web应用程序,在主类中扩展
*SpringBootServletInitializer*
。 案例3: 如果使用
spring boot starter webflux
,则还可以添加
spring boot starter web
作为依赖项。

解决方案是:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-tomcat</artifactId>
  <scope>provided</scope> // remove this scope
</dependency>
我在
application.yml
文件中将下面的属性显式设置为
none

spring:
  main:
    web-application-type: none

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

我在迁移到spring boot时遇到了这个问题。我找到了一个很好的方法,这很有帮助。所以,我删除了jspapi项目的依赖项。此外,还必须删除对servlet api的依赖关系

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

可能您在spring boot starter类中缺少
@SpringBootApplication

@SpringBootApplication
public class LoginSecurityAppApplication {

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

}

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

这就是我的工作。只需将默认配置文件添加到application.properties文件


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

使用注释class
public static void main
,例如:
@SpringBootApplication
您可能会在项目中使用它:

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

org.springframework.boot
弹簧启动器webflux
在这种情况下,您还必须添加:

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

org.springframework.boot
SpringBootStarterWeb
奇迹发生了:)


PS:这是因为Spring在默认情况下会使用WebMVC而不是WebFlux,当两者都可用时

在我的例子中,问题是我的eclipse中没有单独安装Tomcat服务器。我假设我的Springboot会自动启动服务器

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

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

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

Run As -> Run on Server

我的解决方案与严重的依赖性有关。我有:

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

org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧启动机tomcat

在我的pom中,我不得不对排除进行注释,以使其正常工作。出于某种原因,它必须查找这个tomcat包。

至于我,我删除了tomcat依赖项中提供的
范围

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

org.springframework.boot
弹簧启动机tomcat
提供//删除此范围

我试图用spring boot创建一个web应用程序,但遇到了相同的错误。 检查之后,我发现我缺少一个依赖项。因此,请确保将以下依赖项添加到pom.xml文件中

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

org.springframework.boot
SpringBootStarterWeb

在我的例子中,问题是在评论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> 

org.springframework.boot
SpringBootStarterWeb

在我的例子中,我使用的是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>
4.0.0
spring启动应用程序

  • -作为模块的Spring Boot应用程序
  • -当spring boot starter web作为依赖项包含时,如果主类扩展了带有@SpringBootApplication注释的基类,则无法加载应用程序

  • 除了其他答案中可能的解决方案外,您的机器上也可能发生Maven依赖性损坏。我在尝试运行我的(Web)Spring启动应用程序时遇到了相同的错误,通过运行以下命令解决了这个问题-

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

    mvn package
    

    我在这个解决方案中研究了另一个问题,Eclipse不允许我从IDE运行主应用程序类,这是由于一个不同的错误,类似于这个线程上的错误,所以添加以下bean对我来说是有效的

        @Bean
        public ServletWebServerFactory servletWebServerFactory() {
            return new TomcatServletWebServerFactory();
        }
    
    我使用
    SpringApplication.run(MyApplication.class,args)运行非WebSpring应用程序不带
    @springbootplication
    注释

  • @SpringBootApplication:已检查,未工作
  • Pom文件中的spring boot starter web:已检查,未工作
  • 从IntelliJ CE切换到IntelliJ Ultimate,问题解决

  • 与确保安装了
    org.springframework.boot:springbootstartertomcat
    的解决方案类似,我在
    build.gradle


    org.springframework.boot:spring boot starter web
    需要一个服务器,无论是Tomcat、Jetty还是其他什么——它可以编译,但不能在没有服务器的情况下运行。

    我在IntelliJ IDEA中右键单击了我的项目,然后Maven->Reload project,问题解决了。

    以防您正在使用IntelliJ,而这正发生在您身上(就像我的noob一样),确保运行设置有Spring启动应用程序,而不是普通应用程序。

    我的问题与原来的问题相同,只是我是通过Eclipse运行的,而不是cmd.T
    start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>
    
    <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>