Java Spring Boot:由于缺少 EmbeddedServletContainerFactory bean 无法启动 EmbeddedWebApplicationContext
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21783391/
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 Boot: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
提问by Kumite
I am totally new to Spring and started to do the official guides from this site: https://spring.io/guides
我对 Spring 完全陌生,并开始从这个网站做官方指南:https: //spring.io/guides
I'd like to do this guide: https://spring.io/guides/gs/scheduling-tasks/
我想做这个指南:https: //spring.io/guides/gs/scheduling-tasks/
I get the following Exception:
我收到以下异常:
2014-02-14 16:25:21.614 INFO 9032 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.scheduling.annotation.SchedulingConfiguration' of type [class org.springframework.scheduling.annotation.SchedulingConfiguration$$EnhancerByCGLIB$b48d763] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2014-02-14 16:25:21.638 INFO 9032 --- [ main] .c.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/C:/work/Spring/SpringTutorial/target/classes/, file:/C:/work/apache-maven-3.0.3/repo/javax/servlet/javax.servlet-api/3.0.1/javax.servlet-api-3.0.1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter/1.0.0.RC1/spring-boot-starter-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot/1.0.0.RC1/spring-boot-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-core/4.0.0.RELEASE/spring-core-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-context/4.0.0.RELEASE/spring-context-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-autoconfigure/1.0.0.RC1/spring-boot-autoconfigure-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-logging/1.0.0.RC1/spring-boot-starter-logging-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/jcl-over-slf4j/1.7.5/jcl-over-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/slf4j-api/1.7.5/slf4j-api-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/jul-to-slf4j/1.7.5/jul-to-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/org/slf4j/log4j-over-slf4j/1.7.5/log4j-over-slf4j-1.7.5.jar, file:/C:/work/apache-maven-3.0.3/repo/ch/qos/logback/logback-classic/1.0.13/logback-classic-1.0.13.jar, file:/C:/work/apache-maven-3.0.3/repo/ch/qos/logback/logback-core/1.0.13/logback-core-1.0.13.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-web/1.0.0.RC1/spring-boot-starter-web-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/boot/spring-boot-starter-tomcat/1.0.0.RC1/spring-boot-starter-tomcat-1.0.0.RC1.jar, file:/C:/work/apache-maven-3.0.3/repo/org/apache/tomcat/embed/tomcat-embed-core/7.0.47/tomcat-embed-core-7.0.47.jar, file:/C:/work/apache-maven-3.0.3/repo/org/apache/tomcat/embed/tomcat-embed-logging-juli/7.0.47/tomcat-embed-logging-juli-7.0.47.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-web/4.0.0.RELEASE/spring-web-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-aop/4.0.0.RELEASE/spring-aop-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-beans/4.0.0.RELEASE/spring-beans-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-webmvc/4.0.0.RELEASE/spring-webmvc-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/org/springframework/spring-expression/4.0.0.RELEASE/spring-expression-4.0.0.RELEASE.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/Hymanson/core/Hymanson-databind/2.3.1/Hymanson-databind-2.3.1.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/Hymanson/core/Hymanson-annotations/2.3.0/Hymanson-annotations-2.3.0.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/Hymanson/core/Hymanson-core/2.3.1/Hymanson-core-2.3.1.jar, file:/C:/work/apache-maven-3.0.3/repo/commons-lang/commons-lang/2.2/commons-lang-2.2.jar]
Exception in thread "main" 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:140)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:658)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:355)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:920)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:909)
at hu.kumite.Application.main(Application.java:17)
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:190)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:163)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
... 7 more
The application starter class is this:
应用程序入门类是这样的:
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}
As you can see, the main method contains a commented line. I've already done a tutorial, namely this one: https://spring.io/guides/gs/consuming-rest/It's up and running. But I can't run the ScheduledTasks app, which is the following:
如您所见,main 方法包含一个注释行。我已经完成了一个教程,即这个:https: //spring.io/guides/gs/sumption-rest/它已启动并运行。但我无法运行 ScheduledTasks 应用程序,如下所示:
@EnableScheduling
public class ScheduledTasks {
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
System.out.println("The time is now " + dateFormat.format(new Date()));
}
}
I use Eclipse and run the Application.java's main as an Application. Could someone please help me?
我使用 Eclipse 并将 Application.java 的 main 作为应用程序运行。有人可以帮我吗?
采纳答案by Dave Syer
The scheduling guide isn't a web app so you probably have some mouldy stuff in your pom.xml from the REST guide? If you follow the instructions closely it should work. Another potential issue with the code you posted above is that your @EnableAutoConfiguration
class is not used in the context, only as a main method (which may not be a problem for the scheduling guide but it probably is for a bunch of others).
调度指南不是一个网络应用程序,所以你的 pom.xml 中可能有一些来自 REST 指南的发霉的东西?如果您严格按照说明操作,它应该会起作用。您上面发布的代码的另一个潜在问题是您的@EnableAutoConfiguration
类未在上下文中使用,仅用作主要方法(这可能不是调度指南的问题,但可能是其他一些问题)。
回答by Ahmad AlMughrabi
Try this
尝试这个
@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}
回答by HClark
A scan of the @SpringBootApplication
show that it includes the following annotations:
对@SpringBootApplication
节目的扫描,它包括以下注释:
@Configuration
@ComponentScan
@EnableAutoConfiguration
So you could do this too:
所以你也可以这样做:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}
回答by Sagar
Add
添加
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
回答by afaulconbridge
I've had similar problems when the main method is on a different class than that passed to SpringApplcation.run()
当 main 方法位于与传递给 SpringApplcation.run() 的类不同的类上时,我遇到了类似的问题
So the solution would be to use the line you've commented out:
因此,解决方案是使用您注释掉的行:
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
回答by Pasha GR
use this one in your pom.xml :
在你的 pom.xml 中使用这个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
or this one :
或者这个:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
回答by Binhong Wu
If you run it successfully using command line gradle bootRun
, while packaging it with command line gradle jar
to jar file in order to run it with command line java -jar build/libs/demo.jar
, unfortunately, it failed with Exception: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
, in this case, you need to use task bootRepackage
of gradle plugin spring-boot
to generate special runnable jar.
如果您使用命令行成功运行它gradle bootRun
,同时使用命令行将其打包gradle jar
到 jar 文件以便使用命令行运行它java -jar build/libs/demo.jar
,不幸的是,它失败了 Exception: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
,在这种情况下,您需要使用bootRepackage
gradle 插件的任务spring-boot
来生成特殊的可运行的罐子。
- setup 1
$ gradle clean bootRepackage
- setup 2
$ java -jar build/libs/demo.jar
- 设置 1
$ gradle clean boot重新打包
- 设置 2
$ java -jar build/libs/demo.jar
回答by Pavan Kumar Patil
If you package it as a single jar and it's non web app try to load app context as below.
如果您将其打包为单个 jar 并且它不是 Web 应用程序,请尝试加载应用程序上下文,如下所示。
@SpringBootApplication
ApplicationContext ctx = new AnnotationConfigApplicationContext(Main.class);
Or use below plugin to package as a single jar
或使用以下插件打包为单个 jar
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
you can specify the external configs by using below command to run
您可以使用以下命令指定外部配置来运行
java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties
Note that if you are passing the properties as arguments then don't include @PropertySource("classpath:test.properties")
it will override the parameters
请注意,如果您将属性作为参数传递,则不包含@PropertySource("classpath:test.properties")
它将覆盖参数
回答by Thomas Mwania
The error suggests that the application you are trying to run cannot instantiate an instance of apache tomcat. Make sure you are running the application with tomcat.
该错误表明您尝试运行的应用程序无法实例化 apache tomcat 的实例。确保您正在使用 tomcat 运行应用程序。
if after checking all your dependencies you experience the same problem, try to add the following in your configuration class
如果在检查完所有依赖项后遇到同样的问题,请尝试在配置类中添加以下内容
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory =
new TomcatEmbeddedServletContainerFactory();
return factory;
}
If you are using an external instance of tomcat (especially for intellij), the problem could be that the IDE is trying to start the embedded tomcat. In this case, remove the following from your pom.xml then configure the external tomcat using the 'Edit Configurations' wizard.
如果您使用的是 tomcat 的外部实例(特别是对于 intellij),则问题可能是 IDE 正在尝试启动嵌入式 tomcat。在这种情况下,从 pom.xml 中删除以下内容,然后使用“编辑配置”向导配置外部 tomcat。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
回答by leimbag
A SpringApplication
will attempt to create the right type of ApplicationContext
on your behalf. By default, an AnnotationConfigApplicationContext
or AnnotationConfigEmbeddedWebApplicationContext
will be used, depending on whether you are developing a web application or not.
ASpringApplication
将尝试ApplicationContext
代表您创建正确的类型。默认情况下,将使用AnnotationConfigApplicationContext
或AnnotationConfigEmbeddedWebApplicationContext
,具体取决于您是否正在开发 Web 应用程序。
The algorithm used to determine a ‘web environment' is fairly simplistic (based on the presence of a few classes). You can use setWebEnvironment(boolean webEnvironment)
if you need to override the default.
用于确定“网络环境”的算法相当简单(基于几个类的存在)。setWebEnvironment(boolean webEnvironment)
如果需要覆盖默认值,可以使用。
It is also possible to take complete control of the ApplicationContext
type that will be used by calling setApplicationContextClass(…?)
.
还可以ApplicationContext
通过调用完全控制将使用的类型setApplicationContextClass(…?)
。
[Tip]
It is often desirable to call setWebEnvironment(false)
when using SpringApplication
within a JUnit test.
[提示]在 JUnit 测试中setWebEnvironment(false)
使用时通常需要调用SpringApplication
。