【问题标题】:Spring Boot Project cannot run by IntelliJ IDE [duplicate]Spring Boot 项目无法由 IntelliJ IDE 运行 [重复]
【发布时间】:2016-11-29 05:33:01
【问题描述】:

编辑: 这是 WebApplication 文件:

@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) 运行一个 spring boot 项目,

当我执行java -jar spring-boot-sample.war 时它工作正常 不幸的是,它未能由 IDE 运行并抱怨 Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean

错误详情如下:

[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 设置中的某些内容?

【问题讨论】:

  • 您似乎正在尝试使用 spring boot 应用程序类 运行它。你也可以分享一下吗?您也可以尝试取消注释&lt;scope&gt;provided&lt;/scope&gt; from the spring-boot-starter-tomcat`,以便在启动应用程序时它将使用嵌入式tomcat并重试?
  • @Morfic 请检查我的编辑,我发布了申请文件;我从 spring-boot-starter-tomcat 中注释掉了 provided,不幸的是,它仍然无法正常工作
  • 在类定义前使用@SpringBootApplication注解
  • 就这些了吗?就像@PankajKumar 建议的那样,你是如何配置你的 spring 上下文、java-annotation 或 XML 的?
  • 一切似乎都很好......只需尝试添加该注释......因为你的春天没有开始......这个注释有助于启动

标签: java intellij-idea spring-boot


【解决方案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/

【讨论】:

  • @RickChen 你能提供一些有效的链接吗?你发的那个是空的。顺便说一句,我正在使用 Intellij Ultimate
  • 并不是要高人一等,但您有任何官方文档来支持您的you'll need a tomcat server configuration in IJ after adding spring-boot-starter-tomcat 声明吗?据我所知,情况正好相反,它将为您提供embedded tomcat preconfigured to make it work out of the box,因此您可以从主类 运行您的应用程序。毕竟,我相信这就是 boot 被固执己见的原因,我错了吗?
  • @HaifengZhang 如果您删除了最后的],则链接有效,这很可能是不幸的格式错误
  • @Morfic 感谢您更正链接。我已经更新了答案。至于嵌入式与在容器中运行的问题,您说得对,引导可以做到这两点。由于 OP 没有指定整个 pom 文件让我查看包装标签,因此我基于它产生了一个 war 而不是一个 jar 的事实做出了一个假设,这会产生他遇到的 Embed 错误。跨度>
【解决方案2】:

作为一种快速解决方法,您可以添加一个 jar 配置,该配置使用目标文件夹中生成的 jar。

这样就可以正常启动应用或者调试模式了。

这种方法的一个缺点是:您将无法使用热插拔,并且只有在构建 maven 之后才能看到更改。但在这种情况下,我通常会在添加 jar 运行配置窗口之前的部分添加一个带有“clean package -DskipTests”的 maven 目标,因此每次按运行时,我都会获得最新的更改。

这是一个快速修复;这个问题可以通过其他方式解决——我以前做过,但我不记得到底是什么有问题的依赖——你需要在 Spring 源代码中挖掘一点,并围绕异常跳转的方法进行一些调试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 1970-01-01
    • 2020-09-25
    • 1970-01-01
    • 2019-06-17
    • 2018-11-12
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多