2014-02-14 98 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); 
    } 
} 
相關問題