错误:由于缺少EmbeddedServletContainerFactory bean,无法启动EmbeddedWebApplicationContext

时间:2016-01-29 06:20:54

标签: java maven jar spring-boot

我已经创建了一个Spring启动应用程序,它在eclipse IDE中运行良好,但是当我尝试将它构建为带有maven的独立jar时,它给了我这个错误。

我的POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>rcubems</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- tag::actuator[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- end::actuator[] -->
        <!-- tag::tests[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- end::tests[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
            <version>2.9.1</version>
        </dependency>

    </dependencies>

    <properties>
        <java.version>1.7</java.version>
        <start-class>com.my.rcms.Application</start-class>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.my.rcms.Application</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <repositories>
        <repository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <name>Spring Releases</name>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
</project>

我的主要班级:

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);


    }

}

我发现,通过遵循两个命令

创建了依赖关系
* mvn clean install    
* mvn clean compile assembly:single

产生一个具有依赖性的jar,但不包括其中的普通jar。非常感谢帮助。

错误追踪:

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) ~[rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [rcubems-0.1.0-jar-with-dependencies.jar:na]
    at com.my.rcms.Application.main(Application.java:12) [rcubems-0.1.0-jar-with-dependencies.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:185) ~[rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[rcubems-0.1.0-jar-with-dependencies.jar:na]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[rcubems-0.1.0-jar-with-dependencies.jar:na]
    ... 8 common frames omitted

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我通过添加以下依赖项来解决它:

<properties>
    <jetty.version>9.2.17.v20160517</jetty.version>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

希望这可以帮到你(虽然很久以前就遇到过这个问题)。