Spring Boot Web服务器在Eclipse中工作正常,无法在Server上启动:缺少EmbeddedServletContainerFactory bean
按照 Spring文档的模型,我创建了一个非常简单的Hello像世界一样的应用程序.它在Eclipse上旋转,一切看起来都很棒.甜的!我运行它并可以浏览到URL.有史以来最快的发展.
Following the model of the Spring Documentation I created a very simple Hello World like application. It spun right up on Eclipse and everything looked great. Sweet! I ran it and could browse to the URL. Fastest development ever.
但是它必须在服务器上运行,然后查看jar,它只有大约4K,所以我知道没有一堆classpath配置就无法工作.为了避免这种情况,我认为我需要一个带有依赖的jar.
But this has to run on a server, and looking at the jar, it was only about 4K so I knew that it wasn't going to work without a bunch of classpath configuration. To avoid that, I figured I needed a jar-with-dependencies jar.
这就是我的pom.xml,除了添加了程序集插件的jar-with-dependencies目标之外,基本上与Spring示例相同.
So this is my pom.xml, basically identical to that of the Spring example except adding the jar-with-dependencies goal for the assembly plugin.
<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>
但是当我运行该东西时:
But when I go to run the thing:
java -jar LoadTestController-1.0.0-jar-with-dependencies.jar
我得到:
org.springframework.context.ApplicationContextException:无法执行 启动嵌入式容器;嵌套的异常是 org.springframework.context.ApplicationContextException:无法执行 由于缺少启动EmbeddedWebApplicationContext EmbeddedServletContainerFactory bean.
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.
我认为这没有什么区别,但这是该项目中的一个(也是唯一一个)类:
I don't think it should make any difference but here is the one (and only) class in this project:
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);
}
我做错了什么?
答案是... 不要使用maven-assembly-plugin.在文档,我们看到Spring为此制作了自己的插件spring-boot-maven-plugin.更改pom.xml以删除maven-assembly-plugin并改为使用:
And the answer is ... Don't use the maven-assembly-plugin. Reading further on in the documentation, we see that Spring has made their own plugin for this, the spring-boot-maven-plugin. Changing the pom.xml to remove the maven-assembly-plugin and use instead:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
解决了问题.
我决定不提这个问题,以防其他人遇到这个问题.您以为自己会做某事,但是时间在前进.
I decided to leave this question up in case anyone else runs into this problem. You think you know how to do something, but time marches on.