首页 文章

Spring Boot:由于缺少EmbeddedServletContainerFactory bean,无法启动EmbeddedWebApplicationContext

提问于
浏览
155

我是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方法包含注释行 . 我已经完成了一个教程,即这一个:https://spring.io/guides/gs/consuming-rest/它's up and running. But I can' t运行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()));
    }
}

我使用Eclipse并将Application.java的main作为Application运行 . 有人可以帮帮我吗?

26 回答

  • 0

    我有一个类似的问题,问题是一个破碎的maven repo jar文件 . 就我而言, tomcat-embed-core jar文件被破坏了 . 所以我将它从maven repo中删除并刷新以便再次下载 .

  • 2

    在我的情况下,它使用以下代码从 pom 中排除资源文件夹后发生 .

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

    评论此代码开始了我的代码 .

  • 2

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

    mvn dependency:purge-local-repository
    
  • 4

    可能你在spring boot starter class中缺少 @SpringBootApplication .

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

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

  • 7

    扫描 @SpringBootApplication 表明它包含以下注释:

    @Configuration
    @ComponentScan
    @EnableAutoConfiguration
    

    所以你也可以这样做:

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

    在你的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>
    
  • 61

    试试这个

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

    该错误表明您尝试运行的应用程序无法实例化apache tomcat的实例 . 确保使用tomcat运行应用程序 .

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

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

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

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

    我在一个Spring Boot项目中有多个应用程序类,其中包含了Web,并希望避免为其中一个配置Web环境,因此我手动配置如下:

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

    UPDATE for Spring Boot 2及以上版本:

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

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

    如果您在使用intellij时遇到此异常,并且您尝试使用 run 按钮启动该应用程序 . 尝试从命令行启动应用程序 . 例如 . 确保你在正确的目录(带有你的pom文件的目录)中,假设这是一个 springboot 应用程序运行 mvn spring-boot:run 这对我有用 .

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

  • 0

    添加注释 @SpringBootApplication 在入门类之前为我修复了这个问题(所以实际上,这个错误信息可能意味着“你没有任何地方 @SpringBootApplication 标记类,你至少需要一个)

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

    当main方法与传递给SpringApplcation.run()的类不同时,我遇到了类似的问题

    因此,解决方案是使用您已注释掉的行:

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

    如果您将其打包为单个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") 它将覆盖参数

  • 22

    如果使用命令行 gradle bootRun 成功运行它,同时使用命令行 gradle jar 将其打包到jar文件以便使用命令行 java -jar build/libs/demo.jar 运行它,遗憾的是,它失败并显示异常: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean ,在这种情况下,您需要使用任务 bootRepackage of gradle plugin spring-boot 生成特殊的runnable jar .

    • 设置1

    $ gradle clean bootRepackage

    • 设置2

    $ java -jar build / libs / demo.jar

  • 0

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

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

    也可以完全控制 ApplicationContext 类型通过调用 setApplicationContextClass(…) 使用 .

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

  • 2

    添加spring boot starter依赖项修复了我的错误 .

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

    如果要将tomcat作为嵌入式服务器启动,则必须执行此操作 .

  • 2

    检查你的pom.xml是否存在

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

    我有这样的问题;因为缺乏这种依赖性

  • 0

    在我的情况下,我们 added the @Profile annotation newly 为了忽略 生产环境 模式下的TestApplication类和测试模式下的Application类 .

    不幸的是,我们忘记将以下行添加到application.properties文件中:

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

    没有这些配置,没有加载配置文件,导致不太多的说Spring Error .

  • 1

    这应该是由依赖性问题引起的,一般来说,您需要检查依赖性 .

  • 0

    这个课程中存在的问题:

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

    Clear repository 是一种可能的解决方案 .

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

    C:\ Users \ YourUserName.m2 \ repository

  • 28

    问题是初学者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>
    
  • 0

    在以下情况下我有这个例外 .

    在我的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”)的名称和路径是错误的 .

  • 0

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

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

相关问题