2014-02-14 99 views
113

我完全陌生的春天,开始从这个网站做了官方指南: https://spring.io/guides春季启动:无法启动EmbeddedWebApplicationContext由于缺少EmbeddedServletContainerFactory豆

我想这样做指南: 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(Application.class, args); 
    SpringApplication.run(ScheduledTasks.class, args); 
    } 
} 

正如您所见,主要方法包含注释行。我已经完成了一个教程,即这一个:https://spring.io/guides/gs/consuming-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())); 
    } 
} 

我使用Eclipse和运行Application.java的主要为应用程序。 有人可以帮我吗?

回答

44

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

+1

我正在将所有教程做成一个Eclipse项目,并以一个巨大的pom结尾。 我把它放在一个单独的项目中,使用教程中的pom,现在它完美地工作。 感谢您的帮助! 如果我想要使用此API的Spring webapp中的调度器,该怎么办?我不能那样做? – Kumite

+0

你可以做到这一点的来源。但是,然后你会需要所有的webapp的依赖。 –

+0

但是最后我得到了可以在线程中看到的异常。 对不起,我想我错过了一些东西。我应该在这里发布“糟糕的”pom.xml吗? – Kumite

20

试试这个

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

Solve我的问题,谢谢... – Kartoch

37

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

@Configuration 
@ComponentScan 
@EnableAutoConfiguration 

所以,你可以这样做太:

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

添加

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

我有类似的问题的主要方法是在不同的类比传递到SpringApplcation.run()

因此,解决办法是使用行你注释掉:

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

使用一个在你的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> 
2

如果你运行它使用命令行gradle bootRun成功,而为了与命令行java -jar build/libs/demo.jar运行命令行gradle jar其打包到jar文件,不幸的是,它具有异常失败:Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean,在这种情况下,你需要使用任务bootRepackage的gradle插件spring-boot生成特殊的可运行jar。

  • 设置1

    $摇篮干净bootRepackage

  • 设置2

    $ Java的罐子建立/库/ Demo.jar文件

+0

我不知道为什么,但'gradle clean bootRepackage'帮助了我。以前,我已经手动清理所有缓存并生成文件夹。 –

2

如果你把它打包成一个单独的jar,它不是web应用程序尝试加载app context如下。

@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> 

您可以使用以下命令指定外部CONFIGS运行

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")它将覆盖参数

+0

添加spring-boot-maven-plugin和,然后运行应用程序为mvn clean package为我工作。 – hipokito

11

错误提示您尝试运行的应用程序无法实例化apache tomcat的实例。确保你正在用tomcat运行应用程序。

如果检查所有的依赖,你遇到同样的问题后,尝试添加以下配置中的类

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

如果您使用的是Tomcat(尤其是intelij)的外部情况,问题可能是IDE试图启动嵌入式tomcat。在这种情况下,请从pom.xml中删除以下内容,然后使用“编辑配置”向导配置外部tomcat。

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

这对我有效,后来又得到了一个错误,但是这个错误至少已经消失了。 – Manish

+1

很好的回答!它解决了我的问题。然而它在intellij工作。当我使用maven遮罩插件创建jar时,创建了相同的类路径,所以我不太了解发生了什么。 – apprentice

1

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

用于确定“Web环境”的算法相当简单(基于少数类的存在)。如果您需要覆盖默认值,则可以使用setWebEnvironment(boolean webEnvironment)

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

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

+0

如何通过'SpringApplication.run(Application.class,args);'创建应用程序时使用setWebEnvironment? – tgdavies

+0

@tgdavies看到我上面的答案,你可以使用'SpringApplicationBuilder' –

13

我曾在一个春天启动项目中的多个应用程序类,其中有网络开始列入,并希望避免它配置的网络环境,为他们中的一个,所以我手动将其如下配置:

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

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

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

+0

重新启动IntelliJ(Ultimate 2016.3这里)为我解决了这个问题 – mangusbrother

2

添加注释@SpringBootApplication在初学者类为我解决这个问题之前。

@SpringBootApplication 
public class AppStarter { 

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

添加spring引导启动器依赖项修复了我的错误。

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

如果要将tomcat作为嵌入式服务器启动,则这是必需的。

0

在我的情况下,我们新增了@Profile注释,以便忽略生产模式下的TestApplication类和测试模式下的Application类。

不幸的是,我们忘了添加下面一行到application.properties文件:

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

没有这些配置没有配置文件加载这引起了不那么多话说春天错误。

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

清除库是一个可能的解决方案。

窗口 - >删除Maven仓库中所有子文件夹:

C:\用户\ YourUserName.m2 \库

+0

对于Java Spring上的PluralSight教程之后的任何人,这是唯一适用于我的解决方案! –

2

检查你的pom.xml是存在

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

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

+0

这解决了我的问题。 – lenhhoxung

0

问题m是排除starter的tomcat,我试着排除它并使用vert。x,所以当我整合机智春季管理员,启动问题

<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> 

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

0

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

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