无法在IntelliJ Idea中启动spring-boot应用程序

16

概述

我可以使用Spring Boot的Maven插件运行该应用程序,但无法在IDE中运行。以下几行详细说明了我的程序。

应用程序类如下:

package com.blss.retailServices.dataExportRouter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.velocity.VelocityAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan
@EnableAutoConfiguration(exclude = { VelocityAutoConfiguration.class })
public class DataExportRouterApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(DataExportRouterApplication.class, args);
    }
}

异常堆栈跟踪如下:

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) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:532) ~[spring-context-4.2.5.RELEASE.jar:4.2.5.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:766) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:361) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180) [spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at com.blss.retailServices.dataExportRouter.DataExportRouterApplication.main(DataExportRouterApplication.java:14) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_74]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_74]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_74]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_74]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:na]
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) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130) ~[spring-boot-1.3.3.RELEASE.jar:1.3.3.RELEASE]
    ... 13 common frames omitted

我使用了@SpringbootApplication,但没有效果。

问题

所以我希望有人能帮助我解决这个问题。


你能展示一下你的pom.xml文件吗? - alexbt
1个回答

29

我猜你正在使用Maven进行war打包项目,并将你的servlet引擎(tomcat)标记为provided。

不幸的是,IntellIJ IDEA在运行时类路径中没有添加provided依赖项, 这就是为什么当你从IDE中运行项目时它无法正常工作的原因。

更新于7/30/2019

根据Giorgio的说法:

他们在应用程序运行配置中添加了一个复选框,可以“包含提供的范围依赖项”


4
非常感谢。这正是我的问题所在。我将“spring-boot-starter-tomcat”依赖范围更改为编译,然后它成功运行了。 - saeedj
9
他们在应用程序运行配置中添加了一个复选框,用于“包括‘提供的’范围内的依赖项”。 - Oskar
@Giorgio,你能告诉我在哪个版本的IntelliJ中看到这个选项吗? - Ena
感谢您解决了我的问题,我使用的是Windows 21H1和IntelliJ build 2021.2.3 (CE) & OpenJDK 64位服务器VM Temurin-11.0.12+7。 - Jankiel Goldman
非常感谢。它解决了我的问题,我很困惑为什么我的代码在STS中运行而在Intellij IDEA中失败。 - Vignesh Ravichandran
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接