spring ApplicationContextException:由于缺少 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27223388/
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
ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
提问by Raghuveer
Trying to setup a simple web application scheduler. So here is my configuration:
尝试设置一个简单的 Web 应用程序调度程序。所以这是我的配置:
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>scheduler</artifactId>
<version>0.1.0</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.1.7.RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<start-class>hello.Application</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>hello.Application</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
My classes look like below
我的课程如下所示
@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application implements WebApplicationInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(ScheduledTasks.class);
}
public void onStartup(ServletContext arg0) throws ServletException {
SpringApplication.run(ScheduledTasks.class);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setPort(8081);
factory.setSessionTimeout(50, TimeUnit.MINUTES);
return factory;
}
}
@EnableScheduling
public class ScheduledTasks {
@Scheduled(fixedRate = 500)
public void reportCurrentTime() {
System.out.println("Testing successful ");
}
}
But when i try to start the container i see the exception
但是当我尝试启动容器时,我看到了异常
ERROR 2592 --- [ost-startStop-1] o.s.boot.SpringApplication : Application startup failed
...
...
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:174)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:147)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:121)
... 20 more
I tried to follow Spring Boot Testing: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory beanbut in vain.
Kindly suggest, thanks
请推荐,谢谢
回答by Andy Wilkinson
You are only running ScheduledTasks:
你只是在运行ScheduledTasks:
SpringApplication.run(ScheduledTasks.class);
when you should run:
什么时候应该运行:
SpringApplication.run(Application.class);
Running ScheduledTasks means that Spring Boot doesn't know about the configuration in Application. Crucially, it means that it doesn't see @EnableAutoConfigurationwhich is what switches on auto-configuration and, because you have Tomcat on the classpath, creates the embedded Tomcat instance.
运行 ScheduledTasks 意味着 Spring Boot 不知道Application. 至关重要的是,这意味着它看不到@EnableAutoConfiguration哪个是自动配置的开关,并且因为类路径上有 Tomcat,所以它创建了嵌入式 Tomcat 实例。
I would fix this by moving the @EnableSchedulingannotation from ScheduledTasksto Application, running Application.classrather than ScheduledTasks.class, and annotating ScheduledTaskswith @Component:
我会通过移动解决这个问题@EnableScheduling,从注释ScheduledTasks到Application,运行Application.class而不是ScheduledTasks.class,并标注ScheduledTasks有@Component:
Application.class:
应用程序类:
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableScheduling
public class Application implements WebApplicationInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
// …
}
ScheduledTasks.class:
ScheduledTasks.class:
@Component
public class ScheduledTasks {
@Scheduled(fixedRate = 500)
public void reportCurrentTime() {
System.out.println("Testing successful ");
}
}
Moving @EnableSchedulingmeans that its on a configuration class, where it belongs. Running Application.classmeans that Spring Boot sees all of its configuration, including enabling component scanning and enabling auto-configuration. Annotating ScheduledTaskswith @Componentmeans that it's found by component scanning and its @Scheduledmethod is detected.
移动@EnableScheduling意味着它在它所属的配置类上。运行Application.class意味着 Spring Boot 可以看到它的所有配置,包括启用组件扫描和启用自动配置。注释ScheduledTasks与@Component装置,它的由组件扫描发现及其@Scheduled检测方法。
This should get things up and running. I would also correct your pom so that you're not using a mixture of versions of Spring Boot (you have 1.1.6 and 1.1.7 in there at the moment) and get rid of your EmbeddedServletContainerFactorybean in favour of configuring the port and session timeout using application.propertiesin src/main/resourcesinstead:
这应该可以启动并运行。我也会更正你的 pom,这样你就不会混合使用 Spring Boot 的版本(你现在有 1.1.6 和 1.1.7)并摆脱你的EmbeddedServletContainerFactorybean 以支持配置端口和会话超时使用application.propertiesinsrc/main/resources代替:
application.properties:
应用程序属性:
server.port = 8081
server.session-timeout = 3000
回答by sarmahdi
besides what @Andy Wilkinson pointed out which is true in the OP's case but not in mine
除了@Andy Wilkinson 指出的,在 OP 的案例中是正确的,但在我的案例中则不然
I saw that my spring-boot-starter-tomcat was with
我看到我的 spring-boot-starter-tomcat 与
<scope>provided</scope>
( as i was adding a servlet initializer to run it on wildfly) i commented out that bit and removed provided from spring-boot-starter-tomcat dependency and it started working. Adding here just in case some one else has the same issue
(因为我正在添加一个 servlet 初始值设定项以在 Wildfly 上运行它)我注释掉了该位并从 spring-boot-starter-tomcat 依赖项中删除了提供,然后它开始工作。添加这里以防万一其他人有同样的问题
回答by Abhishek Sengupta
My one got fixed using this dependency:
我的使用此依赖项得到了修复:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</dependency>

