SpringBoot: 由于丢失了 EmbeddedServletContainerFactory bean,无法启动 EmbeddedWebApplicationContext

我对 Spring 完全不熟悉,从这个网站开始做官方指南: Https://spring.io/guides

我想做这个指南: Https://spring.io/guides/gs/scheduling-tasks/

我得到了以下例外:

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$$5b48d763] 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/jackson/core/jackson-databind/2.3.1/jackson-databind-2.3.1.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-annotations/2.3.0/jackson-annotations-2.3.0.jar, file:/C:/work/apache-maven-3.0.3/repo/com/fasterxml/jackson/core/jackson-core/2.3.1/jackson-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

应用程序启动器类是这样的:

@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}

如您所见,main 方法包含一个注释行。我已经做了一个教程,就是这个: < a href = “ https://spring.io/guide/gs/using-rest/”rel = “ norefrer”> https://spring.io/guides/gs/consuming-rest/ 它已经启动并运行了,但是我无法运行调度任务应用程序,如下所示:

@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()));
}
}

我使用 Eclipse 并将 Application.java 的 main 作为应用程序运行。 有人能帮帮我吗?

413142 次浏览

调度指南不是一个 web 应用程序,所以你可能在你的 pom.xml 中有一些来自 REST 指南的发霉的东西?如果你严格按照说明书操作,它应该可以工作。上面发布的代码的另一个潜在问题是,您的 @EnableAutoConfiguration类没有在上下文中使用,只是作为一个主方法使用(这对于调度指南可能不是问题,但对于其他一些方法可能是问题)。

试试这个

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}

@SpringBootApplication的扫描显示,它包括以下注释:

@Configuration
@ComponentScan
@EnableAutoConfiguration

所以你也可以这样做:

@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(ScheduledTasks.class, args);
}
}

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

当 main 方法所在的类与传递给 SpringAppliccation.run ()的类不同时,我也遇到过类似的问题

所以解决方案就是使用你注释掉的那一行:

public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

在 pom.xml 中使用这个例子:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

或者这个:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

如果您使用命令行 gradle bootRun成功地运行它,同时将它与命令行 gradle jar打包为 jar 文件,以便用命令行 java -jar build/libs/demo.jar运行它,不幸的是,它失败了与例外: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean,在这种情况下,您需要使用任务 bootRepackage的 gradle 插件 spring-boot来生成特殊的可运行 jar。

  • 设定1

    $gradle clean bootRepackage

  • 设定2

    $java-jar build/libs/demo.jar

如果你把它打包成一个罐子,并且它不是 web 应用,那么试着加载应用上下文,如下所示。

@SpringBootApplication


ApplicationContext ctx = new AnnotationConfigApplicationContext(Main.class);

或者使用下面的插件打包成一个单独的 jar

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

您可以使用下面的命令来运行外部配置

java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

/http://docs.spring.io/spring-boot/docs/current/reference/htmlboot-features-external-config.html#boot-features-external-config-application-property-files

注意,如果将属性作为参数传递,那么不包括 @PropertySource("classpath:test.properties"),它将覆盖参数

该错误表明您试图运行的应用程序无法实例化 apachetomcat 的实例。确保使用 tomcat 运行应用程序。

如果在检查了所有依赖项之后,您遇到了同样的问题,请尝试在配置类中添加以下内容

@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory =
new TomcatEmbeddedServletContainerFactory();
return factory;
}

如果您使用的是 tomcat 的外部实例(特别是 intellij) ,那么问题可能是 IDE 试图启动嵌入的 tomcat。在这种情况下,从 pom.xml 中删除以下内容,然后使用“ Edit Configuration”向导配置外部 tomcat。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

SpringApplication将尝试代表您创建正确类型的 ApplicationContext。默认情况下,将使用 AnnotationConfigApplicationContextAnnotationConfigEmbeddedWebApplicationContext,这取决于您是否正在开发 Web 应用程序。

用于确定“ web 环境”的算法相当简单(基于几个类的存在)。如果需要重写默认值,可以使用 setWebEnvironment(boolean webEnvironment)

也可以完全控制将通过调用 setApplicationContextClass(…​)使用的 ApplicationContext类型。

[提示] 在 JUnit 测试中使用 SpringApplication时,通常需要调用 setWebEnvironment(false)

我在一个 Spring Boot 项目中有多个应用程序类,这个项目已经包含了 web,我想避免它为其中一个项目配置 web 环境,所以我手动配置如下:

@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
new SpringApplicationBuilder(Application.class)
.web(false)
.run(args);
}
}

对于 Spring Boot 2及以上版本更新 :

@SpringBootApplication
public class Application
{
public static void main(String[] args)
{
new SpringApplicationBuilder(Application.class)
.web(WebApplicationType.NONE)
.run(args);
}
}

如果您在使用 intellij 时遇到此异常,并试图使用 run按钮启动应用程序。尝试从命令行启动应用程序。例如,假设这是一个运行 springboot应用程序的 mvn spring-boot:run,确保您在正确的目录(包含您的 pom 文件的目录)中,这为我做到了这一点。

此外,我还看到当您的 Spring 应用程序依赖于另一个应用程序时会发生此错误。在这种情况下,我必须先启动另一个应用程序,然后再运行。

添加注释 @SpringBootApplication在初学者类为我解决了这个问题之前(所以本质上,这个错误消息可能意味着“您在任何地方都没有标记为 @SpringBootApplication的类,您至少需要一个类)

@SpringBootApplication
public class AppStarter {


public static void main(String[] args) {
SpringApplication.run(AppStarter.class, args);
}
}

添加弹簧启动器依赖项修复了我的错误。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

如果希望将 tomcat 作为嵌入式服务器启动,则需要这样做。

在我的例子中,我们使用 新添加了@Profile 注释来忽略处于生产模式的 TestApplication 类和处于测试模式的 Application 类。

遗憾的是,我们忘记在 application.properties 文件中添加以下代码行:

spring.profiles.active=test
or
spring.profiles.active=production

如果没有这些配置,就不会加载任何配置文件,从而导致不那么明显的 Spring 错误。

这应该是由依赖性问题引起的,通常,您需要检查依赖性。

这门课的问题是:

@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
//SpringApplication.run(Application.class, args);
SpringApplication.run(ScheduledTasks.class, args);
}
}

启动应用程序的正确方法是:

@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

清除存储库 是一种可能的解决方案。

Windows-> 删除 maven 存储库中的所有子文件夹:

C: Users YourUserName.m2仓库

检查 pom.xml 是否存在

    <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

我有过这样的问题,因为缺乏这种依赖

问题是排除了 starter tomcat,我尝试排除它并使用 vert.x,所以当我与 Spring Admin 集成时,出现了问题

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>

在下面的情况中我遇到了这个异常。

在我的 POM 是属性:

<properties>
<java.version>1.8</java.version>
<!-- The main class to start by executing java -jar -->
<start-class>com.scmaer.java.microservice.Application</start-class>
<cxf.version>3.1.5</cxf.version>
<olingo.version>2.0.10</olingo.version>
<spring.boot.version>1.4.7.RELEASE</spring.boot.version>
<spring.boot.plugin.version>1.5.8.RELEASE</spring.boot.plugin.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<skipTests>false</skipTests>
</properties>

我的应用程序类(“ start-class”)的名称和路径是错误的。

我也遇到过同样的问题,因为我没有在 Spring-Boot 使用 Maven 定义 Main.class 和下面的注释:

@SpringBootApplication
public class Main {
public static void main(String args[]){
SpringApplication.run(Main.class, args);
}
}

我有一个类似的问题,问题是一个破碎的 maven 回购 jar 文件。在我的例子中,tomcat-embed-core jar 文件被破坏了。所以我从 maven 回购中删除了它,并重新刷新以便再次下载。

在我的例子中,它发生在使用以下代码从 pom中排除资源文件夹之后。

<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<excludes>
<exclude>*/*.properties</exclude>
</excludes>
</resource>
</resources>

注释这段代码启动了我的代码。

这个问题的另一个原因是 maven 存储库 jar 的损坏,因此您可以使用以下命令来解决这个问题:

mvn dependency:purge-local-repository

也许你在春季启动课上错过了 @ SpringBootApplication

@SpringBootApplication
public class LoginSecurityAppApplication {


public static void main(String[] args) {
SpringApplication.run(LoginSecurityAppApplication.class, args);
}


}

我正在使用 gradle,遇到似乎问题时,我有一个 commandLineRunner 消费卡夫卡主题和健康检查端点接收传入钩子。我花了12个小时才搞清楚,最后发现我用 mybatis-spring-boot-starter 和 spring-boot-starter-web 有些冲突。后来,我直接引入了 mybatis-spring、 mybatis 和 spring-jdbc,而不是 mybatis-spring-boot-starter,程序运行良好。

希望这能帮上忙

在我的例子中,弹簧配置没有像预期的那样加载:

start java -Xms512m -Xmx1024m <and the usual parameters as needed, like PrintGC etc> -Dspring.config.location=<propertiesfiles> -jar <jar>