【问题标题】:Spring Boot webserver works fine in Eclipse, fails to start on Server : missing EmbeddedServletContainerFactory beanSpring Boot 网络服务器在 Eclipse 中工作正常,无法在服务器上启动:缺少 EmbeddedServletContainerFactory bean
【发布时间】:2016-03-17 20:01:50
【问题描述】:

按照Spring Documentation 的模型,我创建了一个非常简单的类似Hello World 的应用程序。它在 Eclipse 上旋转,一切看起来都很棒。甜的!我运行它并可以浏览到 URL。有史以来最快的开发。

但这必须在服务器上运行,并且查看 jar,它只有大约 4K,所以我知道如果没有一堆类路径配置,它就无法工作。为了避免这种情况,我想我需要一个 jar-with-dependencies jar。

所以这是我的 pom.xml,除了为程序集插件添加 jar-with-dependencies 目标之外,基本上与 Spring 示例中的相同。

<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>com.whatever</groupId>
    <artifactId>LoadTestController</artifactId>
    <version>1.0.0</version>
    <name>LoadTestController</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.0.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.5.3</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.whatever.LoadTestController</mainClass>
                            <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                        </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>
</project>

但是当我去运行这个东西时:

java -jar LoadTestController-1.0.0-jar-with-dependencies.jar

我明白了:

org.springframework.context.ApplicationContextException:无法 启动嵌入式容器;嵌套异常是 org.springframework.context.ApplicationContextException:无法 由于缺少而启动 EmbeddedWebApplicationContext EmbeddedServletContainerFactory bean。

我不认为它应该有任何区别,但这是这个项目中的一个(也是唯一一个)类:

package com.whatever;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@EnableAutoConfiguration
public class LoadTestController {

    @RequestMapping("/")
    public void simulator(HttpServletResponse response) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {

        }
        response.setContentType("text/plain");
        response.setStatus(HttpServletResponse.SC_OK);
        try {
            response.getOutputStream().println("0");
            response.flushBuffer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(LoadTestController.class, args);  
    }

我做错了什么?

【问题讨论】:

    标签: spring-boot maven-assembly-plugin spring-boot-maven-plugin jar-with-dependencies


    【解决方案1】:

    答案是…… 不要使用 maven-assembly-plugin。在documentation 中进一步阅读,我们看到 Spring 为此制作了自己的插件,即 spring-boot-maven-plugin。更改 pom.xml 以删除 maven-assembly-plugin 并改用:

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    

    解决问题。

    我决定留下这个问题,以防其他人遇到这个问题。你以为你知道如何做某事,但时间在流逝。

    【讨论】:

      猜你喜欢
      • 2019-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 2014-07-28
      • 1970-01-01
      • 2013-10-21
      • 1970-01-01
      相关资源
      最近更新 更多