2016-07-25 81 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源代码中进行一些调试,并针对异常跳转的方法进行一些调试。