由于缺少 ServletWebServerFactory bean,即使使用 @springbootapplication,我也无法启动 ServletWebServerApplicationCont

Posted

技术标签:

【中文标题】由于缺少 ServletWebServerFactory bean,即使使用 @springbootapplication,我也无法启动 ServletWebServerApplicationContext【英文标题】:I am getting Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean even with @springbootapplication 【发布时间】:2019-04-13 12:49:36 【问题描述】:

这是我的主要课程 即使我使用应该自动装配所有依赖项的 springbootApplication 注释,我也会收到此错误

@EntityScan(basePackages = "com.valentine.model","com.valentine.messenger")
@SpringBootApplication(scanBasePackages = "com.valentine.gram",
    "com.valentine.service", "com.valentine")
@EnableJpaRepositories("com.valentine.dao")
@EnableTransactionManagement
public class MainApp 

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


下面是我的堆栈跟踪和 pom 文件,这是一个模块化项目,所以我发布主 pom 文件

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-11-09 23:10:47.131 ERROR 9266 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:544) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:762) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:398) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:330) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at com.valentine.gram.MainApp.main(MainApp.java:19) [classes/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]
    at  

现在pom文件在下面

  <dependency>
            <groupId>com.valentine.mygram</groupId>
            <artifactId>main-app</artifactId>
            <version>$project.version</version>
        </dependency>

        <!--spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
            <version>2.0.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>$spring.version</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>$spring.version</version>
        </dependency>


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

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

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

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

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

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>$spring.version</version>
        </dependency>

        <!--servlets-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>$javax.servlet-api.version</version>
        </dependency>

关于问题可能是什么以及我如何解决问题的任何建议

【问题讨论】:

你检查过这个***.com/questions/50231736/… 是的,它建议 spring: main: web-application-type: none 但我的是一个网络应用程序,所以我不能对网络应用程序类型说无 @Deadpool 那么还有什么其他选择 是的,问题是依赖版本应该从 boots repo 中获得,而不是手写这个想法来自你的回答谢谢 【参考方案1】:

据我所知,在 spring boot 中没有必要使用像

这样的额外注释
@EntityScan(basePackages = "com.valentine.model","com.valentine.messenger")
@SpringBootApplication(scanBasePackages = "com.valentine.gram",
    "com.valentine.service", "com.valentine")
@EnableJpaRepositories("com.valentine.dao")

我认为@SpringBootApplication 足以处理所有这些注释!

我不知道你为什么在 spring boot 中使用所有这些依赖项

     <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>$spring.version</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>2.0.4.RELEASE</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>$spring.version</version>
                <scope>test</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>$spring.version</version>
            </dependency>

            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>$spring.version</version>
            </dependency>
            <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>$spring.version</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>$spring.version</version>
        </dependency>

所有这些依赖项都是针对 Spring MVC 的,而不是针对 Spring Boot 的!在 Spring Boot 中,不需要提及依赖项的版本。喜欢

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>$javax.servlet-api.version</version>
     </dependency>

你只能这样写,spring boot会自动处理这个!

 <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
    </dependency>

【讨论】:

以上是关于由于缺少 ServletWebServerFactory bean,即使使用 @springbootapplication,我也无法启动 ServletWebServerApplicationCont的主要内容,如果未能解决你的问题,请参考以下文章

Visual Studio 由于缺少调试目标

由于缺少位置权限,应用程序在启动时崩溃

由于缺少对象,DACPAC 无法部署

由于缺少 KIF 类,项目无法构建

“场景由于缺少入口点而无法访问并且没有...” - 解决此警告

由于缺少文件,快照无法使用