Java 由于缺少 EmbeddedServletContainerFactory bean,Spring 应用程序无法启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37423450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Application doesn't start due to missing EmbeddedServletContainerFactory bean
提问by Olaru Vlad
I'm trying to deploy an application on heroku and came past several issues. The application is working in my IDE(Intellij) but when I try to run it with "heroku local -f Procfile.windows" I get this error:
我正在尝试在 heroku 上部署一个应用程序,但遇到了几个问题。该应用程序正在我的 IDE(Intellij)中运行,但是当我尝试使用“heroku local -f Procfile.windows”运行它时,我收到此错误:
11:30:03 PM web.1 | 2016-05-24 23:30:03.491 WARN 10368 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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.
11:30:03 PM web.1 | 2016-05-24 23:30:03.505 ERROR 10368 --- [ main] o.s.boot.SpringApplication : Application startup failed
11:30:03 PM web.1 | 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.
11:30:03 PM web.1 | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133) ~[CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at cinemagic.main.App.main(App.java:27) [CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.
11:30:03 PM web.1 | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185) ~[CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar:1.0-SNAPSHOT]
11:30:03 PM web.1 | ... 8 common frames omitted
The procfile looks like this: web: java -jar target\CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar
procfile 如下所示: web: java -jar target\CinemaMagic-1.0-SNAPSHOT-jar-with-dependencies.jar
Pom.xml:
pom.xml:
<?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>CinemaMagic</groupId>
<artifactId>CinemaMagic</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>cinemagic.main.App</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
Main class:
主要类:
@SpringBootApplication
@ComponentScan("cinemagic")
@EnableJpaRepositories("cinemagic.repositories")
@EntityScan(basePackages = { "cinemagic.model"})
@EnableScheduling
public class App extends WebMvcAutoConfiguration.WebMvcAutoConfigurationAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/").setCachePeriod(0);
super.addResourceHandlers(registry);
}
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
采纳答案by M. Deinum
For starters work with the framework instead of against/around the framework. Your dependencies are a mess and you aren't using the correct plugin.
对于初学者来说,使用框架而不是反对/围绕框架。您的依赖项一团糟,而且您没有使用正确的插件。
First remove all of your plugins and replace with the spring-boot-maven
plugin. (See also the reference guidefor this).
首先删除所有插件并替换为spring-boot-maven
插件。(另请参阅参考指南)。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Next cleanup your dependencies and use the proper (the Spring Boot managed) versions.
接下来清理您的依赖项并使用正确的(Spring Boot 管理的)版本。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.4.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>
</dependencies>
Finally your application class is (somewhat) flawed you don't need that many annotations and you are extending a Spring Boot config class . What you are attempting to do in your overridden method is already provided by default by Spring Boot. So there is no need to do that. Also just put your App
class in the cinematic
top level package and everything else will be detected for you.
最后,您的应用程序类(有些)有缺陷,您不需要那么多注释,并且您正在扩展 Spring Boot 配置类。默认情况下,Spring Boot 已经提供了您在重写方法中尝试执行的操作。所以没有必要这样做。也只需将您的App
课程放在cinematic
顶级包中,其他所有内容都会为您检测到。
@SpringBootApplication
@EnableScheduling
public class App
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Now rebuild your jar and launch it.
现在重建你的 jar 并启动它。
Note:The main issue was the fact that you weren't using the Spring Boot maven plugin but tried to do that yourself.
注意:主要问题是您没有使用 Spring Boot maven 插件,而是尝试自己这样做。
回答by Akon
Goto C:\Users\username.m2\repository\org\springframework\boot
转到 C:\Users\username.m2\repository\org\springframework\boot
and delete spring-boot-autoconfiguration and build your maven again.
并删除 spring-boot-autoconfiguration 并再次构建您的 maven。
It will Work it will generate again.
它将工作它将再次生成。