2016-07-25 39 views
0

編輯: 這裏的WebApplication的文件:春天引導項目無法通過的IntelliJ IDE運行

@SpringBootApplication 
@EnableAsync 
@EnableAutoConfiguration 
public class WebApplication extends SpringBootServletInitializer { 

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

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(WebApplication.class); 
    } 
} 

我使用的IntelliJ(15.0.2)運行彈簧引導工程,

它工作正常當我執行java -jar spring-boot-sample.war 遺憾的是它沒有通過IDE運行,並抱怨無法啓動EmbeddedWebApplicationContext由於缺少EmbeddedServletContainerFactory豆

錯誤詳細信息如下:

[2016-07-25 12:32:46.979] boot - 5719 ERROR [restartedMain] --- SpringApplication: Application startup failed 
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:133) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) 
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) 
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) 
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) 
    at com.rentacoder.WebApplication.main(WebApplication.java:19) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) 
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:185) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) 
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) 
    ... 13 more 
[2016-07-25 12:32:47.014] boot - 5719 INFO [restartedMain] --- ClasspathLoggingApplicationListener: Application failed to start with classpath: [file:/Users/hzhang/work/workplace/IdeaProjects/RHS/target/classes/] 

下面是在POM文件的依賴關係設置:

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.3.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

<dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-security</artifactId> 
     </dependency> 
     <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> 
      <scope>provided</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-mongodb</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter</artifactId> 
      <exclusions> 
       <exclusion> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-logging</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-log4j</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context-support</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
    </dependencies> 

我錯過了在POM設置的東西嗎?

+0

它看起來像你試圖使用_spring啓動應用程序class_運行它。你能分享嗎?你也可以嘗試從'spring-boot-starter-tomcat'中取消註釋'提供的,所以當你啓動應用程序時,它將使用嵌入式tomcat並重試? – Morfic

+0

@Morfic請檢查我的編輯,我發佈應用程序文件;我注意到提供的來自spring-boot-starter-tomcat,不幸的是,它仍然不能正常工作 – haifzhan

+0

USe @SpringBootApplication註解在課堂定義之前 –

回答

-1

你是如何在IntelliJ上設置配置的?由於它是一個WAR文件,你需要有一個Web服務器容器。你的pom有spring-boot-starter-tomcat,所以你需要創建一個Tomcat服務器配置。這是IntelliJ中的一項付費功能,因此您可能無法使用社區版。

一種解決方法是在你的war文件創建一個遠程調試: http://blog.trifork.com/2014/07/14/how-to-remotely-debug-application-running-on-tomcat-from-within-intellij-idea/

+0

@RickChen你能否提供一些有效的鏈接?你發佈的是空的。順便說一句,我正在使用Intellij Ultimate – haifzhan

+0

不試圖光顧,但是你有任何官方文檔來支持你的陳述:'你需要在添加spring-boot-starter-tomcat後在IJ中配置tomcat服務器配置嗎?據我所知它完全相反,它會爲你提供一個[預先配置好的嵌入式tomcat以使其不工作](http://projects.spring.io/spring-boot/),所以你可以運行您的應用程序從_main class_。畢竟,我相信這是開機自檢背後的推理,我錯了嗎? – Morfic

+0

@HaifengZhang如果你在結尾刪除了''',鏈接就可以工作,這很可能是一個不幸的格式錯誤 – Morfic

0

作爲一個快速的解決方法,你可以添加使用從目標文件夾中生成的罐子罐子配置。

通過這種方式,您可以正常啓動應用程序或以調試模式啓動應用程序。

作爲這種方法的缺點:你不會有熱插拔可用,只有在構建maven後纔會看到更改。但在這種情況下,我通常在添加jar運行配置窗口前添加一個「clean package -DskipTests」的maven目標,這樣每次按下run時,我都會得到最新的更改。

這是一個quickfix;這個問題可以通過其他方式解決 - 我之前做過,但我不記得具體問題是什麼,你需要在Spring源代碼中進行一些調試,並針對異常跳轉的方法進行一些調試。