繁体   English   中英

由于缺少 EmbeddedServletContainerFactory bean,Spring Boot 无法启动 EmbeddedWebApplicationContext

[英]Spring Boot Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

我有一个 Spring Boot Web 应用程序,我试图将它作为一个独立的应用程序运行,其中嵌入了 Tomcat 而不是使用容器。

我正在 IntelliJ 中进行开发,并且已将运行配置设置为 Spring Boot,并且该应用程序正在从 IntelliJ 运行。

我在 IntelliJ 中创建了一个工件来创建一个 JAR 文件,并将它设置为包含来自 Maven 的所有依赖项。

但是,当我运行应用程序时出现错误;

14:52:19.303 [main] ERROR o.s.boot.SpringApplication - Application startup failed
org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[NatLocApp.jar:na]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:531) ~[NatLocApp.jar:na]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[NatLocApp.jar:na]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:752) [NatLocApp.jar:na]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:347) [NatLocApp.jar:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [NatLocApp.jar:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [NatLocApp.jar:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [NatLocApp.jar:na]
    at com.nationallocums.Application.main(Application.java:13) [NatLocApp.jar:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:183) ~[NatLocApp.jar:na]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:156) ~[NatLocApp.jar:na]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[NatLocApp.jar:na]
    ... 8 common frames omitted

从我的研究中,我可以看到这与 Tomcat 没有被嵌入或者 Tomcat 的多个实例被包含在最终的 JAR 文件中有关,但我似乎无法解决这个问题。

这是我的 pom.xml 文件的样子;

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

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

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

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

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

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

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

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

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.37</version>
        </dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        <dependency>
            <groupId>net.sf.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>2.3</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.9.2</version>
        </dependency>
    </dependencies>

我已经尝试将 tomcat 依赖项设置为具有“提供”范围并完全删除范围标记,但两者似乎都没有任何区别。

你的 pom.xml 文件中应该有 spring-boot-maven-plugin。 并将包装设置为罐子。

然后你可以:

$ mvn package
$ java -jar target/mymodule-0.0.1-SNAPSHOT.jar
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

解决方法删除这一行:“ <scope>provided</scope>

此处概述了 scope 属性的可能值。 提供的范围描述如下:

这很像 compile,但表明您希望 JDK 或容器在运行时提供依赖项 例如,在为 Java Enterprise Edition 构建 Web 应用程序时,您可以将对 Servlet API 和相关 Java EE API 的依赖项设置为提供的范围,因为 Web 容器提供这些类。 此范围仅在编译和测试类路径上可用,并且不可传递。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM