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