【问题标题】:SpringBoot missing ServletWebServerFactory beanSpringBoot 缺少 ServletWebServerFactory bean
【发布时间】:2020-08-08 14:55:28
【问题描述】:

我不断收到此错误:

原因:org.springframework.context.ApplicationContextException:由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext。

这是我的运行配置中的目标。

package mystuff;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication(scanBasePackages = "mystuff")
public class Runner {

    public static void main(String[] args) {
        new SpringApplicationBuilder(MyApp.class).run(args);
    }

}

这是 MyApp.class:

package mystuff;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyApp {

    @RequestMapping("/")
    public String index() {
        return "Hello World";
    }
}

这是我的 Gradle 构建文件:

plugins {
    java
    id("org.springframework.boot") version "2.2.2.RELEASE"
    id("io.spring.dependency-management") version "1.0.8.RELEASE"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    testCompile("junit", "junit", "4.12")
}

configure<JavaPluginConvention> {
    sourceCompatibility = JavaVersion.VERSION_1_8
}

MyApp.java 和 Runner.java 都在同一个包“mystuff”中。我在 Stackoverflow 上搜索了这里,但我的配置似乎是正确的。

【问题讨论】:

    标签: spring spring-boot


    【解决方案1】:

    你能不能试试

    @SpringBootApplication(scanBasePackages = "mystuff")
    public class Runner {
    
        public static void main(String[] args) {
            SpringApplication.run(Runner.class, args);
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      Runner 类中,替换

      new SpringApplicationBuilder(MyApp.class).run(args);
      

      new SpringApplicationBuilder(MyApp.class).web(WebApplicationType.SERVLET).run(args);
      

      【讨论】:

        【解决方案3】:

        .build() 方法应该在 .run() 之前调用

        请尝试以下代码

        new SpringApplicationBuilder(MyApp.class).build().run(args);
        

        【讨论】:

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